15
15
*/
16
16
package org .labkey .remoteapi ;
17
17
18
- import org .apache .commons .codec .EncoderException ;
19
- import org .apache .commons .codec .net .URLCodec ;
20
18
import 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 ;
27
25
import org .json .simple .JSONObject ;
28
26
import org .json .simple .JSONValue ;
29
27
@@ -256,12 +254,12 @@ private Response(CloseableHttpResponse httpResponse, String contentType, Long co
256
254
257
255
public String getStatusText ()
258
256
{
259
- return _httpResponse .getStatusLine (). getReasonPhrase ();
257
+ return _httpResponse .getReasonPhrase ();
260
258
}
261
259
262
260
public int getStatusCode ()
263
261
{
264
- return _httpResponse .getStatusLine (). getStatusCode ();
262
+ return _httpResponse .getCode ();
265
263
}
266
264
267
265
public String getContentType ()
@@ -328,7 +326,7 @@ protected Response _execute(Connection connection, String folderPath) throws Com
328
326
{
329
327
//construct and initialize the HttpUriRequest
330
328
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 ());
332
330
333
331
//execute the request
334
332
httpResponse = connection .executeRequest (request , getTimeout ());
@@ -448,19 +446,13 @@ protected HttpUriRequest getHttpRequest(Connection connection, String folderPath
448
446
// TODO (Dave 11/11/14 -- as far as I can tell the default of the AuthSchemes is to automatically handle challenges
449
447
// method.setDoAuthentication(true);
450
448
449
+ // NOTE: Fairly sure that the "unescaped" comment below is obsolete
450
+ // TODO: Combine getActionUrl() and addParameters() into a single method
451
451
//construct a URI from the results of the getActionUrl method
452
452
//note that this method returns an unescaped URL, so pass
453
453
//false for the second parameter to escape it
454
454
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 );
464
456
465
457
return createRequest (uri );
466
458
}
@@ -487,7 +479,7 @@ protected HttpUriRequest createRequest(URI uri)
487
479
* @return The URL
488
480
* @throws URISyntaxException if the uri constructed from the parameters is malformed
489
481
*/
490
- protected URI getActionUrl (Connection connection , String folderPath ) throws URISyntaxException
482
+ private URI getActionUrl (Connection connection , String folderPath ) throws URISyntaxException
491
483
{
492
484
URI uri = connection .getBaseURI ();
493
485
@@ -520,60 +512,55 @@ protected URI getActionUrl(Connection connection, String folderPath) throws URIS
520
512
}
521
513
522
514
/**
523
- * Returns the query string portion of the URL for this command.
515
+ * Adds all parameters to the passed URI
524
516
* <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
528
519
*/
529
- protected String getQueryString ( ) throws CommandException
520
+ protected URI addParameters ( URI uri ) throws CommandException , URISyntaxException
530
521
{
531
522
Map <String , Object > params = getParameters ();
532
523
533
524
//add the required version if it's > 0
534
525
if (getRequiredVersion () > 0 )
535
526
params .put (CommonParameters .apiVersion .name (), getRequiredVersion ());
536
527
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 ())
540
534
{
541
- for (String name : params .keySet ())
535
+ Object value = params .get (name );
536
+ if (value instanceof Collection <?> col )
542
537
{
543
- Object value = params .get (name );
544
- if (value instanceof Collection )
538
+ for (Object o : col )
545
539
{
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 );
554
541
}
555
542
}
543
+ else
544
+ {
545
+ addParameter (builder , name , value );
546
+ }
547
+ }
548
+
549
+ try
550
+ {
551
+ return builder .build ();
556
552
}
557
- catch ( EncoderException e )
553
+ catch ( URISyntaxException e )
558
554
{
559
555
throw new CommandException (e .getMessage ());
560
556
}
561
-
562
- return qstring .length () > 0 ? qstring .toString () : null ;
563
557
}
564
558
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 )
567
560
{
568
561
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 );
577
564
}
578
565
579
566
/**
0 commit comments