/** * Add this method for argument capture in test */ @VisibleForTesting public RestRequest build(RestRequestBuilder builder) { return builder.build(); } }
private RestRequestBuilder setCommonRequestHeader(RestRequestBuilder requestBuilder) { requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, String.valueOf(System.currentTimeMillis())); if(this.routingTypeCode != null) { requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, this.routingTypeCode); } if(this.zoneId != INVALID_ZONE_ID) { requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId)); } return requestBuilder; }
/** * Builds HTTP headers related to response compression and creates a RestRequest with those headers added. * * @param responseCompressionOverride compression force on/off override from the request context. * @param req current request. * @return request with response compression headers. */ public RestRequest addResponseCompressionHeaders(CompressionOption responseCompressionOverride, RestRequest req) { RestRequestBuilder builder = req.builder(); if (responseCompressionOverride == null) { builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, _acceptEncodingHeader); if (_responseCompressionConfig != null) { builder.addHeaderValue(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, Integer.toString(_responseCompressionConfig.getCompressionThreshold())); } } else if (responseCompressionOverride == CompressionOption.FORCE_ON) { builder.addHeaderValue(HttpConstants.ACCEPT_ENCODING, _acceptEncodingHeader) .addHeaderValue(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, Integer.toString(0)); } return builder.build(); }
String requestContentEncoding = req.getHeader(HttpConstants.CONTENT_ENCODING); byte[] decompressedContent = encoding.getCompressor().inflate(req.getEntity().asInputStream()); Map<String, String> headers = new HashMap<String, String>(req.getHeaders()); headers.remove(HttpConstants.CONTENT_ENCODING); headers.put(HttpConstants.CONTENT_LENGTH, Integer.toString(decompressedContent.length)); req = req.builder().setEntity(decompressedContent).setHeaders(headers).build(); String responseAcceptedEncodings = req.getHeader(HttpConstants.ACCEPT_ENCODING); if (responseAcceptedEncodings == null) RestResponse restResponse = new RestResponseBuilder().setStatus(HttpConstants.UNSUPPORTED_MEDIA_TYPE).build(); nextFilter.onError(new RestException(restResponse, e), requestContext, wireAttrs);
try { String base64Key = new String(Base64.encodeBase64(getName().getBytes("UTF-8"))); rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + SCHEMATA_STORE_NAME + "/" + base64Key)); rb.setHeader("Accept", "binary"); rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, String.valueOf(System.currentTimeMillis())); if(this.routingTypeCode != null) { rb.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, this.routingTypeCode); rb.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId)); return response.getEntity().asString("UTF-8"); } catch(ExecutionException e) { if(e.getCause() instanceof RestException) { RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
public String getStoreClientConfigString(List<String> storeNames, String coordinatorUrl) { try { // Create the REST request StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl) .append(URL_SEPARATOR) .append(STORE_CLIENT_CONFIG_OPS) .append(URL_SEPARATOR) .append(Joiner.on(",") .join(storeNames)); RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString())); String timeoutStr = Long.toString(this.config.getTimeoutConfig() .getOperationTimeout(VoldemortOpCode.GET_OP_CODE)); requestBuilder.setMethod(requestType.GET.toString()); requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr); requestBuilder = setCommonRequestHeader(requestBuilder); RestRequest request = requestBuilder.build(); Future<RestResponse> future = client.restRequest(request); // This will block RestResponse response = future.get(); ByteString entity = response.getEntity(); return entity.asString("UTF-8"); } catch(Exception e) { if(e.getCause() instanceof RestException) { return ((RestException) e.getCause()).getResponse().getEntity().asString("UTF-8"); } handleRequestAndResponseException(e); } return null; }
if (_helper.shouldCompressRequest(req.getEntity().length(), (CompressionOption) requestContext.getLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE) )) byte[] compressed = compressor.deflate(req.getEntity().asInputStream()); if (compressed.length < req.getEntity().length()) req = req.builder().setEntity(compressed).setHeader(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName()).build();
if (res.getEntity().length() > 0) res.getEntity().length() > (Integer) requestContext.getLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD)) byte[] compressed = compressor.deflate(res.getEntity().asInputStream()); if (compressed.length < res.getEntity().length()) RestResponseBuilder resCompress = res.builder(); resCompress.addHeaderValue(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName()); resCompress.setEntity(compressed); res = resCompress.build(); res = res.builder().setStatus(HttpConstants.NOT_ACCEPTABLE).setEntity(new byte[0]).build();
private RestResponse fetchGetResponse(RestRequestBuilder requestBuilder, String timeoutStr) throws InterruptedException, ExecutionException { requestBuilder.setMethod(GET); requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr); requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, String.valueOf(System.currentTimeMillis())); if(this.routingTypeCode != null) { requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, this.routingTypeCode); } if(this.zoneId != INVALID_ZONE_ID) { requestBuilder.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId)); } RestRequest request = requestBuilder.build(); Future<RestResponse> f = client.restRequest(request); // This will block return f.get(); }
public static void assertEqual(RestRequestBuilder actual, RestRequestBuilder expect) throws IOException { // Check entity ByteString actualEntity = actual.getEntity(); ByteString expectedEntity = expect.getEntity(); if (actualEntity == null) { Assert.assertTrue(expectedEntity == null); } else { Assert.assertEquals(actualEntity.length(), expectedEntity.length()); Assert.assertEquals(actualEntity.asString(StandardCharsets.UTF_8),expectedEntity.asString(StandardCharsets.UTF_8)); } // Check request RestRequest actualRequest = actual.build(); RestRequest expectedRequest = expect.build(); Assert.assertEquals(actualRequest.getMethod(), expectedRequest.getMethod()); Assert.assertEquals(actualRequest.getURI().toString(), expectedRequest.getURI().toString()); Map<String, String> actualHeaders = actualRequest.getHeaders(); Map<String, String> expectedHeaders = expectedRequest.getHeaders(); Assert.assertEquals(actualHeaders.size(), expectedHeaders.size()); for (String key: actualHeaders.keySet()) { Assert.assertEquals(actualHeaders.get(key), expectedHeaders.get(key)); } } }
/** * Add payload to request. By default, payload is sent as application/json */ protected int addPayload(RestRequestBuilder builder, String payload) { if (payload == null || payload.length() == 0) { return 0; } builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_APPLICATION_JSON); try { DataMap data = JACKSON_DATA_CODEC.stringToMap(payload); byte[] bytes = JACKSON_DATA_CODEC.mapToBytes(data); builder.setEntity(bytes); return bytes.length; } catch (IOException e) { throw new RuntimeException("Fail to convert payload: " + payload, e); } }
@Override public String toString() { RestRequest request = getRawRequest(); StringBuilder outBuffer = new StringBuilder(); String endl = "\n"; outBuffer.append("R2Request Info").append(endl); outBuffer.append("type: RestRequest").append(endl); outBuffer.append("uri: ").append(request.getURI().toString()).append(endl); outBuffer.append("headers: "); request.getHeaders().forEach((k, v) -> outBuffer.append("[").append(k).append(":").append(v).append("] ") ); outBuffer.append(endl); ByteString entity = request.getEntity(); if (entity != null) { outBuffer.append("body: ").append(entity.asString(Charset.defaultCharset())).append(endl); } return outBuffer.toString(); } }
@Override public void onResponse(RestResponse restResponse, RequestContext requestContext, Map<String, String> wireAttrs) { String header = restResponse.getHeader(_headerName); Assert.assertEquals(header, _expectedValue); if (_entityLength > 0) { Assert.assertEquals(restResponse.getEntity().length(), _entityLength); } }
private void handleRequestAndResponseException(Exception e) { if(e instanceof ExecutionException) { if(e.getCause() instanceof RestException) { RestException re = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST Exception Status: " + re.getResponse().getStatus()); } } else { throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e); } } else if(e instanceof InterruptedException) { if(logger.isDebugEnabled()) { logger.debug("Operation interrupted : " + e.getMessage(), e); } throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e); } else if(e instanceof URISyntaxException) { throw new VoldemortException("Illegal HTTP URL " + e.getMessage(), e); } else if(e instanceof UnsupportedEncodingException) { throw new VoldemortException("Illegal Encoding Type " + e.getMessage(), e); } else { throw new VoldemortException("Unknown exception: " + e.getMessage(), e); } }
@Override public void onRequest(RestRequest restRequest, RequestContext requestContext, Map<String, String> wireAttrs) { String header = restRequest.getHeader(_headerName); Assert.assertEquals(header, _expectedValue); if (_entityLength > 0) { Assert.assertEquals(restRequest.getEntity().length(), _entityLength); } }
@Override protected void fillHttpOutputData(Schema schema, GenericRecord outputRecord, RestRequest restRequest, ResponseStatus status) throws IOException { R2ResponseStatus r2ResponseStatus = (R2ResponseStatus) status; HttpRequestResponseRecord record = new HttpRequestResponseRecord(); record.setRequestUrl(restRequest.getURI().toASCIIString()); record.setMethod(restRequest.getMethod()); record.setStatusCode(r2ResponseStatus.getStatusCode()); record.setContentType(r2ResponseStatus.getContentType()); record.setBody(r2ResponseStatus.getContent() == null? null: r2ResponseStatus.getContent().asByteBuffer()); outputRecord.put("HttpRequestResponse", record); }
@Override public void sendAsyncRequestImpl(RestRequest request, Callback<RestResponse> callback) throws IOException { log.debug ("Request URI : {} ", request.getURI()); client.restRequest(request, new com.linkedin.common.callback.Callback<RestResponse>() { @Override public void onError(Throwable e) { callback.onFailure(e); } @Override public void onSuccess(RestResponse result) { callback.onSuccess(result); } }); }
@Override public RestResponse sendRequestImpl(RestRequest request) throws IOException { Future<RestResponse> responseFuture = client.restRequest(request); RestResponse response; try { response = responseFuture.get(); } catch (InterruptedException | ExecutionException e) { // The service may choose to throw an exception as a way to report error Throwable t = e.getCause(); if (t != null && t instanceof RestException) { response = ((RestException) t).getResponse(); } else { throw new IOException(e); } } return response; }
RestRequestBuilder rb = null; try { rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + base64Key)); String timeoutStr = Long.toString(this.config.getTimeoutConfig() .getOperationTimeout(VoldemortOpCode.GET_OP_CODE)); rb.setHeader("Accept", MULTIPART_CONTENT_TYPE); final ByteString entity = response.getEntity(); if(entity != null) { resultList = parseGetResponse(entity); RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
RestRequestBuilder rb = null; try { rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/" + getName() + "/" + base64Key)); String timeoutStr = Long.toString(this.config.getTimeoutConfig() .getOperationTimeout(VoldemortOpCode.GET_VERSION_OP_CODE)); rb.setHeader(RestMessageHeaders.X_VOLD_GET_VERSION, "true"); final ByteString entity = response.getEntity(); if(entity != null) { resultList = parseGetVersionResponse(entity); RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());