List of usage examples for io.netty.handler.codec.http QueryStringEncoder toUri
public URI toUri() throws URISyntaxException
From source file:io.gravitee.gateway.core.invoker.EndpointInvoker.java
License:Apache License
private URI encodeQueryParameters(String uri, MultiValueMap<String, String> parameters) throws URISyntaxException { if (parameters != null && !parameters.isEmpty()) { QueryStringEncoder encoder = new QueryStringEncoder(uri); for (Map.Entry<String, List<String>> queryParam : parameters.entrySet()) { if (queryParam.getValue() != null) { for (String value : queryParam.getValue()) { encoder.addParam(queryParam.getKey(), (value != null && !value.isEmpty()) ? value : null); }/*from w ww . ja va2s. c om*/ } } return encoder.toUri(); } return URI.create(uri); }
From source file:org.waarp.gateway.kernel.rest.client.HttpRestClientHelper.java
License:Open Source License
/** * Send an HTTP query using the channel for target, using signature * /*from w w w . ja v a2 s.co m*/ * @param hmacSha256 * SHA-256 key to create the signature * @param channel * target of the query * @param method * HttpMethod to use * @param host * target of the query (shall be the same as for the channel) * @param addedUri * additional uri, added to baseUri (shall include also extra arguments) (might be null) * @param user * user to use in authenticated Rest procedure (might be null) * @param pwd * password to use in authenticated Rest procedure (might be null) * @param uriArgs * arguments for Uri if any (might be null) * @param json * json to send as body in the request (might be null); Useful in PUT, POST but should not in GET, DELETE, OPTIONS * @return the RestFuture associated with this request */ public RestFuture sendQuery(HmacSha256 hmacSha256, Channel channel, HttpMethod method, String host, String addedUri, String user, String pwd, Map<String, String> uriArgs, String json) { // Prepare the HTTP request. logger.debug("Prepare request: " + method + ":" + addedUri + ":" + json); RestFuture future = channel.attr(HttpRestClientSimpleResponseHandler.RESTARGUMENT).get(); QueryStringEncoder encoder = null; if (addedUri != null) { encoder = new QueryStringEncoder(baseUri + addedUri); } else { encoder = new QueryStringEncoder(baseUri); } // add Form attribute if (uriArgs != null) { for (Entry<String, String> elt : uriArgs.entrySet()) { encoder.addParam(elt.getKey(), elt.getValue()); } } String[] result = null; try { result = RestArgument.getBaseAuthent(hmacSha256, encoder, user, pwd); logger.debug("Authent encoded"); } catch (HttpInvalidAuthenticationException e) { logger.error(e.getMessage(), e); future.setFailure(e); return future; } URI uri; try { uri = encoder.toUri(); } catch (URISyntaxException e) { logger.error(e.getMessage()); future.setFailure(e); return future; } logger.debug("Uri ready: " + uri.toASCIIString()); FullHttpRequest request; if (json != null) { logger.debug("Add body"); ByteBuf buffer = Unpooled.wrappedBuffer(json.getBytes(WaarpStringUtils.UTF8)); request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, uri.toASCIIString(), buffer); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, buffer.readableBytes()); } else { request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, uri.toASCIIString()); } // it is legal to add directly header or cookie into the request until finalize request.headers().add(this.headers); request.headers().set(HttpHeaderNames.HOST, host); if (user != null) { request.headers().set((CharSequence) RestArgument.REST_ROOT_FIELD.ARG_X_AUTH_USER.field, user); } request.headers().set((CharSequence) RestArgument.REST_ROOT_FIELD.ARG_X_AUTH_TIMESTAMP.field, result[0]); request.headers().set((CharSequence) RestArgument.REST_ROOT_FIELD.ARG_X_AUTH_KEY.field, result[1]); // send request logger.debug("Send request"); channel.writeAndFlush(request); logger.debug("Request sent"); return future; }
From source file:org.waarp.gateway.kernel.rest.client.HttpRestClientHelper.java
License:Open Source License
/** * Send an HTTP query using the channel for target, but without any Signature * /*from www. ja v a2s .c om*/ * @param channel * target of the query * @param method * HttpMethod to use * @param host * target of the query (shall be the same as for the channel) * @param addedUri * additional uri, added to baseUri (shall include also extra arguments) (might be null) * @param user * user to use in authenticated Rest procedure (might be null) * @param uriArgs * arguments for Uri if any (might be null) * @param json * json to send as body in the request (might be null); Useful in PUT, POST but should not in GET, DELETE, OPTIONS * @return the RestFuture associated with this request */ public RestFuture sendQuery(Channel channel, HttpMethod method, String host, String addedUri, String user, Map<String, String> uriArgs, String json) { // Prepare the HTTP request. logger.debug("Prepare request: " + method + ":" + addedUri + ":" + json); RestFuture future = channel.attr(HttpRestClientSimpleResponseHandler.RESTARGUMENT).get(); QueryStringEncoder encoder = null; if (addedUri != null) { encoder = new QueryStringEncoder(baseUri + addedUri); } else { encoder = new QueryStringEncoder(baseUri); } // add Form attribute if (uriArgs != null) { for (Entry<String, String> elt : uriArgs.entrySet()) { encoder.addParam(elt.getKey(), elt.getValue()); } } URI uri; try { uri = encoder.toUri(); } catch (URISyntaxException e) { logger.error(e.getMessage()); future.setFailure(e); return future; } logger.debug("Uri ready: " + uri.toASCIIString()); FullHttpRequest request; if (json != null) { logger.debug("Add body"); ByteBuf buffer = Unpooled.wrappedBuffer(json.getBytes(WaarpStringUtils.UTF8)); request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, uri.toASCIIString(), buffer); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, buffer.readableBytes()); } else { request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, uri.toASCIIString()); } // it is legal to add directly header or cookie into the request until finalize request.headers().add(this.headers); request.headers().set(HttpHeaderNames.HOST, host); if (user != null) { request.headers().set((CharSequence) RestArgument.REST_ROOT_FIELD.ARG_X_AUTH_USER.field, user); } request.headers().set((CharSequence) RestArgument.REST_ROOT_FIELD.ARG_X_AUTH_TIMESTAMP.field, new DateTime().toString()); // send request logger.debug("Send request"); channel.writeAndFlush(request); logger.debug("Request sent"); return future; }