/** * Helper method for modiffying the URI path * @param uri * @param newPath * @return */ public static String modifyURIPath(String uri, String newPath) { try { URI uriObj = new URI(uri); return uriToString(URLUtils.modifyURIPath(uriObj, newPath)); } catch (URISyntaxException e) { try { return (new NetworkInterfaceURI(uri)).modifyURIPath(newPath); } catch (IllegalArgumentException ne) { throw new IllegalArgumentException(ne.getMessage(), ne); } } }
/** * Follows a redirect. * @param httpSession * @param session * @return * @throws URISyntaxException */ private DefaultConnectFuture followRedirect(DefaultHttpSession httpSession, IoSessionEx session) throws URISyntaxException { HashMap<ResourceOption<?>, Object> overrides = new HashMap<>(); // fix redirects Integer maxRedirects = new Integer((httpSession.getRemoteAddress().getOption(MAXIMUM_REDIRECTS)) - 1); overrides.put(MAXIMUM_REDIRECTS, maxRedirects); // fix location URI newLocation = new URI(httpSession.getReadHeader("location")); URI oldLocation = httpSession.getRequestURL(); newLocation = resolveFragement(newLocation, oldLocation); if (!newLocation.isAbsolute()) { newLocation = URLUtils.modifyURIPath(oldLocation, newLocation.getPath()); } String oldQuery = oldLocation.getQuery(); if (oldQuery != null && appendQueryWhenFollowingRedirect) { newLocation = URLUtils.modifyURIQuery(newLocation, oldQuery); } httpSession.setRequestURI(URLUtils.getPathAndQueryURI(newLocation)); ResourceAddress newConnectAddress = addressFactory.newResourceAddress(newLocation.toString(), new WrappedResourceOptionsForConnectionRetry(httpSession, overrides)); return retryConnect(httpSession, session, newConnectAddress); }
private ResourceAddress createApiHttpAddress(ResourceAddress httpTransport) { String path = URIUtils.getPath(appendURI(ensureTrailingSlash(httpTransport.getExternalURI()), HttpProtocolCompatibilityFilter.API_PATH)); String httpLocation = modifyURIPath(httpTransport.getExternalURI(), path); ResourceOptions httpOptions = ResourceOptions.FACTORY.newResourceOptions(httpTransport); httpOptions.setOption(NEXT_PROTOCOL, null); // terminal endpoint, so next protocol null httpOptions.setOption(ALTERNATE, null); return resourceAddressFactory.newResourceAddress(httpLocation, httpOptions); }
private ResourceAddress createApiHttpxeAddress(ResourceAddress httpxeTransport) { String path = URIUtils.getPath(appendURI(ensureTrailingSlash(httpxeTransport.getExternalURI()), HttpProtocolCompatibilityFilter.API_PATH)); httpxeTransport = httpxeTransport.resolve(path); String httpxeLocation = modifyURIPath(httpxeTransport.getExternalURI(), path); ResourceOptions httpxeOptions = ResourceOptions.FACTORY.newResourceOptions(httpxeTransport); httpxeOptions.setOption(NEXT_PROTOCOL, null); // terminal endpoint, so next protocol null return resourceAddressFactory.newResourceAddress(httpxeLocation, httpxeOptions); }