1515 */
1616package org .labkey .remoteapi ;
1717
18- import org .apache .commons .codec .EncoderException ;
19- import org .apache .commons .codec .net .URLCodec ;
2018import org .apache .commons .logging .LogFactory ;
21- import org .apache .http .Header ;
22- import org .apache .http .auth . AuthenticationException ;
23- import org .apache .http .client .methods .CloseableHttpResponse ;
24- import org .apache .http .client . methods . HttpGet ;
25- import org .apache .http . client . methods . HttpUriRequest ;
26- import org .apache .http . client . utils .URIBuilder ;
19+ import org .apache .hc . client5 . http .auth . AuthenticationException ;
20+ import org .apache .hc . client5 . http .classic . methods . HttpGet ;
21+ import org .apache .hc . client5 . http .classic .methods .HttpUriRequest ;
22+ import org .apache .hc . client5 . http .impl . classic . CloseableHttpResponse ;
23+ import org .apache .hc . core5 . http . Header ;
24+ import org .apache .hc . core5 . net .URIBuilder ;
2725import org .json .simple .JSONObject ;
2826import org .json .simple .JSONValue ;
2927
@@ -256,12 +254,12 @@ private Response(CloseableHttpResponse httpResponse, String contentType, Long co
256254
257255 public String getStatusText ()
258256 {
259- return _httpResponse .getStatusLine (). getReasonPhrase ();
257+ return _httpResponse .getReasonPhrase ();
260258 }
261259
262260 public int getStatusCode ()
263261 {
264- return _httpResponse .getStatusLine (). getStatusCode ();
262+ return _httpResponse .getCode ();
265263 }
266264
267265 public String getContentType ()
@@ -328,7 +326,7 @@ protected Response _execute(Connection connection, String folderPath) throws Com
328326 {
329327 //construct and initialize the HttpUriRequest
330328 request = getHttpRequest (connection , folderPath );
331- LogFactory .getLog (Command .class ).info ("Requesting URL: " + request .getURI (). toString ());
329+ LogFactory .getLog (Command .class ).info ("Requesting URL: " + request .getRequestUri ());
332330
333331 //execute the request
334332 httpResponse = connection .executeRequest (request , getTimeout ());
@@ -448,19 +446,13 @@ protected HttpUriRequest getHttpRequest(Connection connection, String folderPath
448446// TODO (Dave 11/11/14 -- as far as I can tell the default of the AuthSchemes is to automatically handle challenges
449447// method.setDoAuthentication(true);
450448
449+ // NOTE: Fairly sure that the "unescaped" comment below is obsolete
450+ // TODO: Combine getActionUrl() and addParameters() into a single method
451451 //construct a URI from the results of the getActionUrl method
452452 //note that this method returns an unescaped URL, so pass
453453 //false for the second parameter to escape it
454454 URI uri = getActionUrl (connection , folderPath );
455-
456- //the query string values will need to be escaped as they are
457- //put into the query string, and we don't want to re-escape the
458- //entire thing, as the %, & and = characters will be escaped again
459- //so add this separately as a raw value
460- //(indicating that it has already been escaped)
461- String queryString = getQueryString ();
462- if (null != queryString )
463- uri = new URIBuilder (uri ).setQuery (queryString ).build ();
455+ uri = addParameters (uri );
464456
465457 return createRequest (uri );
466458 }
@@ -487,7 +479,7 @@ protected HttpUriRequest createRequest(URI uri)
487479 * @return The URL
488480 * @throws URISyntaxException if the uri constructed from the parameters is malformed
489481 */
490- protected URI getActionUrl (Connection connection , String folderPath ) throws URISyntaxException
482+ private URI getActionUrl (Connection connection , String folderPath ) throws URISyntaxException
491483 {
492484 URI uri = connection .getBaseURI ();
493485
@@ -520,60 +512,55 @@ protected URI getActionUrl(Connection connection, String folderPath) throws URIS
520512 }
521513
522514 /**
523- * Returns the query string portion of the URL for this command.
515+ * Adds all parameters to the passed URI
524516 * <p>
525- * The parameters in the query string should be encoded to avoid parsing errors on the server.
526- * @return The query string
527- * @throws CommandException Thrown if there is a problem encoding the query string parameter names or values
517+ * @return The URI with parameters added
518+ * @throws CommandException Thrown if there is a problem building the URI
528519 */
529- protected String getQueryString ( ) throws CommandException
520+ protected URI addParameters ( URI uri ) throws CommandException , URISyntaxException
530521 {
531522 Map <String , Object > params = getParameters ();
532523
533524 //add the required version if it's > 0
534525 if (getRequiredVersion () > 0 )
535526 params .put (CommonParameters .apiVersion .name (), getRequiredVersion ());
536527
537- StringBuilder qstring = new StringBuilder ();
538- URLCodec urlCodec = new URLCodec ();
539- try
528+ if (params .isEmpty ())
529+ return uri ;
530+
531+ URIBuilder builder = new URIBuilder (uri );
532+
533+ for (String name : params .keySet ())
540534 {
541- for (String name : params .keySet ())
535+ Object value = params .get (name );
536+ if (value instanceof Collection <?> col )
542537 {
543- Object value = params .get (name );
544- if (value instanceof Collection )
538+ for (Object o : col )
545539 {
546- for (Object o : ((Collection ) value ))
547- {
548- appendParameter (qstring , urlCodec , name , o );
549- }
550- }
551- else
552- {
553- appendParameter (qstring , urlCodec , name , value );
540+ addParameter (builder , name , o );
554541 }
555542 }
543+ else
544+ {
545+ addParameter (builder , name , value );
546+ }
547+ }
548+
549+ try
550+ {
551+ return builder .build ();
556552 }
557- catch ( EncoderException e )
553+ catch ( URISyntaxException e )
558554 {
559555 throw new CommandException (e .getMessage ());
560556 }
561-
562- return qstring .length () > 0 ? qstring .toString () : null ;
563557 }
564558
565- private void appendParameter (StringBuilder qstring , URLCodec urlCodec , String name , Object value )
566- throws EncoderException
559+ private void addParameter (URIBuilder builder , String name , Object value )
567560 {
568561 String strValue = null == value ? null : getParamValueAsString (value , name );
569- if (null != strValue )
570- {
571- if (qstring .length () > 0 )
572- qstring .append ('&' );
573- qstring .append (urlCodec .encode (name ));
574- qstring .append ('=' );
575- qstring .append (urlCodec .encode (strValue ));
576- }
562+ if (null != strValue )
563+ builder .addParameter (name , strValue );
577564 }
578565
579566 /**
0 commit comments