use of java.net.URISyntaxException in project XobotOS by xamarin.
the class ProxySelectorRoutePlanner method determineProxy.
/**
* Determines a proxy for the given target.
*
* @param target the planned target, never <code>null</code>
* @param request the request to be sent, never <code>null</code>
* @param context the context, or <code>null</code>
*
* @return the proxy to use, or <code>null</code> for a direct route
*
* @throws HttpException
* in case of system proxy settings that cannot be handled
*/
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
// the proxy selector can be 'unset', so we better deal with null here
ProxySelector psel = this.proxySelector;
if (psel == null)
psel = ProxySelector.getDefault();
if (psel == null)
return null;
URI targetURI = null;
try {
targetURI = new URI(target.toURI());
} catch (URISyntaxException usx) {
throw new HttpException("Cannot convert host to URI: " + target, usx);
}
List<Proxy> proxies = psel.select(targetURI);
Proxy p = chooseProxy(proxies, target, request, context);
HttpHost result = null;
if (p.type() == Proxy.Type.HTTP) {
// convert the socket address to an HttpHost
if (!(p.address() instanceof InetSocketAddress)) {
throw new HttpException("Unable to handle non-Inet proxy address: " + p.address());
}
final InetSocketAddress isa = (InetSocketAddress) p.address();
// assume default scheme (http)
result = new HttpHost(getHost(isa), isa.getPort());
}
return result;
}
use of java.net.URISyntaxException in project XobotOS by xamarin.
the class DefaultRedirectHandler method getLocationURI.
public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
//get the location header to find out where to redirect to
Header locationHeader = response.getFirstHeader("location");
if (locationHeader == null) {
// got a redirect response, but no location header
throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location header");
}
String location = locationHeader.getValue();
if (this.log.isDebugEnabled()) {
this.log.debug("Redirect requested to location '" + location + "'");
}
URI uri;
try {
uri = new URI(location);
} catch (URISyntaxException ex) {
throw new ProtocolException("Invalid redirect URI: " + location, ex);
}
HttpParams params = response.getParams();
// Location = "Location" ":" absoluteURI
if (!uri.isAbsolute()) {
if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
}
// Adjust location URI
HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if (target == null) {
throw new IllegalStateException("Target host not available " + "in the HTTP context");
}
HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
try {
URI requestURI = new URI(request.getRequestLine().getUri());
URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
uri = URIUtils.resolve(absoluteRequestURI, uri);
} catch (URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
}
if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);
if (redirectLocations == null) {
redirectLocations = new RedirectLocations();
context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
}
URI redirectURI;
if (uri.getFragment() != null) {
try {
HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
redirectURI = URIUtils.rewriteURI(uri, target, true);
} catch (URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
} else {
redirectURI = uri;
}
if (redirectLocations.contains(redirectURI)) {
throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
} else {
redirectLocations.add(redirectURI);
}
}
return uri;
}
use of java.net.URISyntaxException in project XobotOS by xamarin.
the class DefaultRequestDirector method rewriteRequestURI.
protected void rewriteRequestURI(final RequestWrapper request, final HttpRoute route) throws ProtocolException {
try {
URI uri = request.getURI();
if (route.getProxyHost() != null && !route.isTunnelled()) {
// Make sure the request URI is absolute
if (!uri.isAbsolute()) {
HttpHost target = route.getTargetHost();
uri = URIUtils.rewriteURI(uri, target);
request.setURI(uri);
}
} else {
// Make sure the request URI is relative
if (uri.isAbsolute()) {
uri = URIUtils.rewriteURI(uri, null);
request.setURI(uri);
}
}
} catch (URISyntaxException ex) {
throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(), ex);
}
}
use of java.net.URISyntaxException in project platformlayer by platformlayer.
the class JdbcConfiguration method buildSimple.
private static JdbcConfiguration buildSimple(Configuration configuration, String key) {
String value = configuration.find(key);
if (value == null) {
throw new IllegalStateException("Must define environment variable: " + key);
}
URI dbUri;
try {
dbUri = new URI(value);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Error parsing database environment variable: " + key, e);
}
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String jdbcUrl = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath() + ":" + dbUri.getPort();
String driverClassName = org.postgresql.Driver.class.getName();
Map<String, String> extraProperties = Maps.newHashMap();
// Properties extraProperties = System.getProperties();
JdbcConfiguration jdbcConfig = new JdbcConfiguration(jdbcUrl, username, password, driverClassName, extraProperties);
return jdbcConfig;
}
use of java.net.URISyntaxException in project powermock by powermock.
the class AgentInitialization method getPathToJarFileContainingThisClass.
private String getPathToJarFileContainingThisClass() {
CodeSource codeSource = AgentInitialization.class.getProtectionDomain().getCodeSource();
if (codeSource == null) {
return null;
}
// URI is needed to deal with spaces and non-ASCII characters
URI jarFileURI;
try {
jarFileURI = codeSource.getLocation().toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return new File(jarFileURI).getPath();
}
Aggregations