11/*
22 * Copyright (C) 2011 lightcouch.org
3- * Copyright (c) 2015 IBM Corp. All rights reserved.
3+ * Copyright © 2015, 2021 IBM Corp. All rights reserved.
44 *
55 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
66 * except in compliance with the License. You may obtain a copy of the License at
1515
1616package com .cloudant .client .org .lightcouch ;
1717
18+ import static com .cloudant .client .org .lightcouch .internal .CouchDbUtil .assertDocumentTypeId ;
1819import static com .cloudant .client .org .lightcouch .internal .CouchDbUtil .assertNotEmpty ;
1920import static com .cloudant .client .org .lightcouch .internal .CouchDbUtil .assertNull ;
21+ import static com .cloudant .client .org .lightcouch .internal .CouchDbUtil .assertValidAttachmentName ;
2022import static com .cloudant .client .org .lightcouch .internal .CouchDbUtil .close ;
2123import static com .cloudant .client .org .lightcouch .internal .CouchDbUtil .generateUUID ;
2224import static com .cloudant .client .org .lightcouch .internal .CouchDbUtil .getAsString ;
@@ -81,6 +83,7 @@ public abstract class CouchDatabaseBase {
8183 public <T > T find (Class <T > classType , String id ) {
8284 assertNotEmpty (classType , "Class" );
8385 assertNotEmpty (id , "id" );
86+ assertDocumentTypeId (id );
8487 final URI uri = new DatabaseURIHelper (dbUri ).documentUri (id );
8588 return couchDbClient .get (uri , classType );
8689 }
@@ -98,6 +101,7 @@ public <T> T find(Class<T> classType, String id) {
98101 public <T > T find (Class <T > classType , String id , Params params ) {
99102 assertNotEmpty (classType , "Class" );
100103 assertNotEmpty (id , "id" );
104+ assertDocumentTypeId (id );
101105 final URI uri = new DatabaseURIHelper (dbUri ).documentUri (id , params );
102106 return couchDbClient .get (uri , classType );
103107 }
@@ -116,6 +120,7 @@ public <T> T find(Class<T> classType, String id, String rev) {
116120 assertNotEmpty (classType , "Class" );
117121 assertNotEmpty (id , "id" );
118122 assertNotEmpty (id , "rev" );
123+ assertDocumentTypeId (id );
119124 final URI uri = new DatabaseURIHelper (dbUri ).documentUri (id , "rev" , rev );
120125 return couchDbClient .get (uri , classType );
121126 }
@@ -145,6 +150,7 @@ public <T> T findAny(Class<T> classType, String uri) {
145150 */
146151 public InputStream find (String id ) {
147152 assertNotEmpty (id , "id" );
153+ assertDocumentTypeId (id );
148154 return couchDbClient .get (new DatabaseURIHelper (dbUri ).documentUri (id ));
149155 }
150156
@@ -160,6 +166,7 @@ public InputStream find(String id) {
160166 public InputStream find (String id , String rev ) {
161167 assertNotEmpty (id , "id" );
162168 assertNotEmpty (rev , "rev" );
169+ assertDocumentTypeId (id );
163170 final URI uri = new DatabaseURIHelper (dbUri ).documentUri (id , "rev" , rev );
164171 return couchDbClient .get (uri );
165172 }
@@ -172,6 +179,7 @@ public InputStream find(String id, String rev) {
172179 */
173180 public boolean contains (String id ) {
174181 assertNotEmpty (id , "id" );
182+ assertDocumentTypeId (id );
175183 InputStream response = null ;
176184 try {
177185 response = couchDbClient .head (new DatabaseURIHelper (dbUri ).documentUri (id ));
@@ -255,6 +263,7 @@ public Response remove(Object object) {
255263 public Response remove (String id , String rev ) {
256264 assertNotEmpty (id , "id" );
257265 assertNotEmpty (rev , "rev" );
266+ assertDocumentTypeId (id );
258267 final URI uri = new DatabaseURIHelper (dbUri ).documentUri (id , rev );
259268 return couchDbClient .delete (uri );
260269 }
@@ -308,6 +317,8 @@ public List<Response> bulk(List<?> objects, boolean allOrNothing) {
308317 * @return the attachment in the form of an {@code InputStream}.
309318 */
310319 public InputStream getAttachment (String docId , String attachmentName , String revId ) {
320+ assertDocumentTypeId (docId );
321+ assertValidAttachmentName (attachmentName );
311322 final URI uri = new DatabaseURIHelper (dbUri ).attachmentUri (docId , revId , attachmentName );
312323 return getAttachment (uri );
313324 }
@@ -362,6 +373,7 @@ public Response saveAttachment(InputStream in, String name, String contentType,
362373 String docRev ) {
363374 assertNotEmpty (in , "in" );
364375 assertNotEmpty (name , "name" );
376+ assertValidAttachmentName (name );
365377 assertNotEmpty (contentType , "ContentType" );
366378 if (docId == null ) {
367379 docId = generateUUID ();
@@ -375,6 +387,7 @@ public Response saveAttachment(InputStream in, String name, String contentType,
375387 assertNotEmpty (docRev , "docRev" );
376388 }
377389 }
390+ assertDocumentTypeId (docId );
378391 final URI uri = new DatabaseURIHelper (dbUri ).attachmentUri (docId , docRev , name );
379392 return couchDbClient .put (uri , in , contentType );
380393 }
@@ -410,8 +423,10 @@ public Response removeAttachment(Object object, String attachmentName) {
410423 */
411424 public Response removeAttachment (String id , String rev , String attachmentName ) {
412425 assertNotEmpty (id , "id" );
426+ assertDocumentTypeId (id );
413427 assertNotEmpty (rev , "rev" );
414428 assertNotEmpty (attachmentName , "attachmentName" );
429+ assertValidAttachmentName (attachmentName );
415430 final URI uri = new DatabaseURIHelper (dbUri ).attachmentUri (id , rev , attachmentName );
416431 return couchDbClient .delete (uri );
417432 }
0 commit comments