@@ -61,10 +61,10 @@ protected void removeCol(final String collectionName, final String uid, final St
6161
6262 final Executor exec = getExecutor (uid , pwd );
6363 try {
64- final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (Request .delete (collectionUri )).returnResponse ();
65-
66- if ( resp . getCode () != HttpStatus . SC_OK ) {
67- throw new ApiException ( "Could not remove collection: " + collectionUri + ". " + getResponseBody ( resp . getEntity ()));
64+ try ( final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (Request .delete (collectionUri )).returnResponse ()) {
65+ if ( resp . getCode () != HttpStatus . SC_OK ) {
66+ throw new ApiException ( "Could not remove collection: " + collectionUri + ". " + getResponseBody ( resp . getEntity ()));
67+ }
6868 }
6969 } catch (final IOException ioe ) {
7070 throw new ApiException (ioe );
@@ -103,12 +103,12 @@ protected void addCollectionUserAce(final String collectionUri, final String use
103103 protected String getXmlResourceContent (final String resourceUri , final String uid , final String pwd ) throws ApiException {
104104 final Executor exec = getExecutor (uid , pwd );
105105 try {
106- final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (Request .get (getServerUri () + resourceUri )).returnResponse ();
107-
108- if ( resp . getCode () != HttpStatus . SC_OK ) {
109- throw new ApiException ( "Could not get XML resource from uri: " + resourceUri + ". " + getResponseBody ( resp . getEntity ()));
110- } else {
111- return getResponseBody ( resp . getEntity ());
106+ try ( final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (Request .get (getServerUri () + resourceUri )).returnResponse ()) {
107+ if ( resp . getCode () != HttpStatus . SC_OK ) {
108+ throw new ApiException ( "Could not get XML resource from uri: " + resourceUri + ". " + getResponseBody ( resp . getEntity ()));
109+ } else {
110+ return getResponseBody ( resp . getEntity ());
111+ }
112112 }
113113 } catch (final IOException ioe ) {
114114 throw new ApiException (ioe );
@@ -139,14 +139,15 @@ protected void createGroup(final String group_gid, final String uid, final Strin
139139 protected void createXmlResource (final String resourceUri , final String content , final String uid , final String pwd ) throws ApiException {
140140 final Executor exec = getExecutor (uid , pwd );
141141 try {
142- final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (
142+ try ( final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (
143143 Request .put (getServerUri () + resourceUri )
144144 .addHeader ("Content-Type" , "application/xml" )
145145 .bodyByteArray (content .getBytes ())
146- ).returnResponse ();
147-
148- if (resp .getCode () != HttpStatus .SC_CREATED ) {
149- throw new ApiException ("Could not store XML resource to uri: " + resourceUri + ". " + getResponseBody (resp .getEntity ()));
146+ ).returnResponse ()) {
147+
148+ if (resp .getCode () != HttpStatus .SC_CREATED ) {
149+ throw new ApiException ("Could not store XML resource to uri: " + resourceUri + ". " + getResponseBody (resp .getEntity ()));
150+ }
150151 }
151152 } catch (final IOException ioe ) {
152153 throw new ApiException (ioe );
@@ -157,14 +158,15 @@ protected void createXmlResource(final String resourceUri, final String content,
157158 protected void createBinResource (final String resourceUri , final byte [] content , final String uid , final String pwd ) throws ApiException {
158159 final Executor exec = getExecutor (uid , pwd );
159160 try {
160- final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (
161+ try ( final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (
161162 Request .put (getServerUri () + resourceUri )
162163 .addHeader ("Content-Type" , "application/octet-stream" )
163164 .bodyByteArray (content )
164- ).returnResponse ();
165-
166- if (resp .getCode () != HttpStatus .SC_CREATED ) {
167- throw new ApiException ("Could not store Binary resource to uri: " + resourceUri + ". " + getResponseBody (resp .getEntity ()));
165+ ).returnResponse ()) {
166+
167+ if (resp .getCode () != HttpStatus .SC_CREATED ) {
168+ throw new ApiException ("Could not store Binary resource to uri: " + resourceUri + ". " + getResponseBody (resp .getEntity ()));
169+ }
168170 }
169171 } catch (final IOException ioe ) {
170172 throw new ApiException (ioe );
@@ -175,12 +177,12 @@ private void executeQuery(final String xquery, final String uid, final String pw
175177 final Executor exec = getExecutor (uid , pwd );
176178 try {
177179 final String queryUri = createQueryUri (xquery );
178-
179- final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (Request .get (queryUri )).returnResponse ();
180180
181- final int status = resp .getCode ();
182- if (status != HttpStatus .SC_OK ) {
183- throw new ApiException ("HTTP " + status + " could not execute query uri: " + queryUri + ". " + getResponseBody (resp .getEntity ()));
181+ try (final ClassicHttpResponse resp = (ClassicHttpResponse ) exec .execute (Request .get (queryUri )).returnResponse ()) {
182+ final int status = resp .getCode ();
183+ if (status != HttpStatus .SC_OK ) {
184+ throw new ApiException ("HTTP " + status + " could not execute query uri: " + queryUri + ". " + getResponseBody (resp .getEntity ()));
185+ }
184186 }
185187 } catch (final IOException ioe ) {
186188 throw new ApiException (ioe );
0 commit comments