0

We are using httpclient to connect with a website where there are a lot of redirects.

After we tested our initial implementation, we were getting an ioexception, but when we tried to output the information about the exception, like .getMessage, etc. we were getting just a null.

Then we tried debugging the webapp, using Eclipse remote debugging (the web app is running on WebLogic) and we found that when it got the ioexception, there were actually several "layers" of exceptions that weren't directly visible. It appears that the httpclient is handling several redirects "under the covers" and doesn't trigger our code, even though the app was configured for LaxRedirect:

    CloseableHttpClient httpclient = HttpClients.custom()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setSSLContext(sslContext)
            .setDefaultRequestConfig(requestConfig)
            .build();

From the remote debugging, it looks like, on one of those "hidden" redirects, the app was getting a:

java.net.URISyntaxException: Illegal character in query at index 316:   https://ZZZZZZZZZZZZZZZ.XXXXX.YYY:14430/oam/pages/consent.jsp?state=WDNvK0ltU2plY1J2dkJhSmJBZkV5dz09fi9SQXIrVVk0ZUZ0cW11WU80TEEyMmJibFFBVzNrZm5Jc3dGbHdINy9lTFY5OVhZRll0TzR5Z1hWNjExK3ZPQmZiTmNjcW9sVGVOWWdya3A1bHhmQ3k0REVSRytBbWwvSHlWbjVlUDk4RFoyeXBHZnBBenFOMU5CaDYzV3NTaGlt&scopes=UserProfile.me&client_id=TestClient|eventStack#10103:0,10125:0,10126:0,10205:0,10305:0,10201:0,10117:0|eventFlowControllerStack#ssoFlowController,|authn_try_count#_k=authn_try_count

And it looks like the "illegal character" is the 'pipe' chars (i.e., '|').

Then I found this thread:

HttpClient redirecting to URL with spaces throwing exception

(see the post from "Drakes") which talks about using a custom redirect strategy and overriding the createLocationURI() method, and so we are now trying to do that, to replace the pipe char ('|') with "%7C".

So I changed:

    CloseableHttpClient httpclient = HttpClients.custom()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .setSSLContext(sslContext)
            .setDefaultRequestConfig(requestConfig)
            .build();

to:

    CloseableHttpClient httpclient = HttpClients.custom()
            .setRedirectStrategy(new CleanRedirectStrategy())
            .setSSLContext(sslContext)
            .setDefaultRequestConfig(requestConfig)
            .build();

and made a small class:

class CleanUrlRedirectStrategy extends LaxRedirectStrategy
{

@Override
protected URI createLocationURI( final String orig ) throws ProtocolException
    {
      System.out.println( "++++++++++++++++++++++++++ In CleanUrlRedirectStrategy.createLocationURI V1.01: Entering, orig=[" + orig + "]" );
     String fixed = orig.replaceAll("\\|","%7C"); 

     if ( ! orig.equals( fixed ) )
         {
         System.out.println( "++++++++++++++++++++++++++ In CleanUrlRedirectStrategy.createLocationURI V1.01: FOUND PIPE CHARACTER - Cleaned redirect URL from [" + orig + "] to [" + fixed + "]" );
     } else {
         System.out.println( "++++++++++++++++++++++++++ In CleanUrlRedirectStrategy.createLocationURI V1.01: No PIPE characters found, so did not change orig!!!!" );
     }

 System.out.println( "++++++++++++++++++++++++++ In CleanUrlRedirectStrategy.createLocationURI V1.01: about to RETURN, fixed=[" + fixed + "]" );

 return super.createLocationURI( fixed );
 }
 }// end CLASS CleanUrlRedirectStrategy

When I test, I can see the log output, and I can see that after the 1st http.execute(), the CleanUrlRedirectStrategy.createLocationURI() gets called 3 times, but it doesn't find any pipe chars (that was kind of expected, because from our earlier testing, it looked like the problem was happening when the 2nd http.execute() was done.

I think I then see one more http.execute() attempt, but then the whole apps seems to stop (I don't see any further output in the log).

So I was wondering: Is there something else that I need to do for that override approach to work?

It "feels" like, when I changed the strategy from the Lax strategy to my "Clean" strategy, that maybe my strategy class is no longer following redirects?

Thanks in advance!

Jim

user555303
  • 1,146
  • 3
  • 20
  • 44
  • Minor point, but why are you using `replaceAll` instead of `replace`? You wouldn't have to escape the regex special character with the latter, and you're not actually using regular expressions. – David Conrad Dec 24 '20 at 22:05
  • @DavidConrad - I actually ended up not using replaceAll()/replace(), but tried to do URLEncoder over the querystring, but even that did not solve the problem. I am in a kind of catch-22 situation - If I don't do the URL encoding, then I get the URISyntaxException, because the URI has characters like pipe ("|"), etc. and if I do the URL encoding, there is a JSP later in the protocol (this is an OAuth protocol) that throws an NPE. What I'd really like to do is to override/ignore the URISyntaxException so that the protocol continues processing even though there are such characters in the URIs. – user555303 Dec 28 '20 at 14:02

0 Answers0