private static URI getBaseURI(final String host, final String path) { try { String baseURI = null; if (!StringUtils.hasText(path)) { baseURI = host; } else if (host.endsWith(SentiloConstants.SLASH) && path.startsWith(SentiloConstants.SLASH)) { baseURI = host.substring(0, host.length() - 1) + path; } else if (!host.endsWith(SentiloConstants.SLASH) && !path.startsWith(SentiloConstants.SLASH)) { baseURI = host + SentiloConstants.SLASH + path; } else { baseURI = host + path; } return new URI(baseURI); } catch (final URISyntaxException e) { throw buildIllegalArgumentException(host, path, e); } catch (final NullPointerException npe) { throw buildIllegalArgumentException(host, path, npe); } }
public static URI getURI(final String host, final String path) { return getURI(host, path, null); }
public static URI getURI(final String host, final String path, final RequestParameters parameters) { try { final URI baseURI = getBaseURI(host, path); final URIBuilder builder = new URIBuilder(baseURI); if (parameters != null && parameters.size() > 0) { for (final String key : parameters.keySet()) { final String value = parameters.get(key); builder.setParameter(key, value); } } return builder.build(); } catch (final URISyntaxException e) { throw buildIllegalArgumentException(host, path, e); } }
public String delete(final RequestContext rc) throws RESTClientException { final String targetHost = getRequestTargetHost(rc); // As a request DELETE cannot have body, we simulate the call to DELETE doing a PUT request with // the parameter method==delete final URI uri = StringUtils.hasText(rc.getBody()) ? URIUtils.getURI(targetHost, rc.getPath(), RequestParameters.buildDelete()) : URIUtils.getURI(targetHost, rc.getPath()); final HttpRequestBase delete = StringUtils.hasText(rc.getBody()) ? new HttpPut(uri) : new HttpDelete(uri); return executeHttpCall(delete, rc.getBody(), rc.getIdentityToken()); }
public String post(final RequestContext rc) throws RESTClientException { final String targetHost = getRequestTargetHost(rc); LOGGER.debug("Send post message to host {} and path {}", targetHost, rc.getPath()); LOGGER.debug("Token {}", rc.getIdentityToken()); LOGGER.debug("Body {}", rc.getBody()); final URI uri = URIUtils.getURI(targetHost, rc.getPath()); final HttpPost post = new HttpPost(uri); return executeHttpCall(post, rc.getBody(), rc.getIdentityToken()); }
public String put(final RequestContext rc) throws RESTClientException { final String targetHost = getRequestTargetHost(rc); final URI uri = URIUtils.getURI(targetHost, rc.getPath()); final HttpPut put = new HttpPut(uri); return executeHttpCall(put, rc.getBody(), rc.getIdentityToken()); }
public String get(final RequestContext rc) throws RESTClientException { final String targetHost = getRequestTargetHost(rc); final URI uri = URIUtils.getURI(targetHost, rc.getPath(), rc.getParameters()); final HttpGet get = new HttpGet(uri); return executeHttpCall(get, rc.getIdentityToken()); }