eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType));
entityMethod.setRequestEntity(new BytesArrayRequestEntity(ba)); entityMethod.setContentChunked(false);
.setRequestEntity(requestEntity);
private static EntityEnclosingMethod getMethod(EntityEnclosingMethod method, RequestEntity entity) { if (entity != null) method.setRequestEntity(entity); return method; }
private File prepareProxiedMethodBody(EntityEnclosingMethod method, HttpServletRequest request) throws ServletException, IOException { File requestBodyFile = getRequestBodyAsFile(request); method.setRequestEntity( new InputStreamRequestEntity( new BufferedInputStream(new FileInputStream(requestBodyFile)), requestBodyFile.length(), request.getContentType() ) ); return requestBodyFile; }
/** * Sets up the given {@link org.apache.commons.httpclient.methods.PostMethod} to send the same multipart POST data * as was sent in the given {@link HttpServletRequest} * * @param postMethodProxyRequest The {@link org.apache.commons.httpclient.methods.PostMethod} that we are configuring to send a * multipart POST request * @param httpServletRequest The {@link HttpServletRequest} that contains the multipart * POST data to be sent via the {@link org.apache.commons.httpclient.methods.PostMethod} */ @SuppressWarnings("unchecked") public static void handleMultipartPost( EntityEnclosingMethod postMethodProxyRequest, HttpServletRequest httpServletRequest, DiskFileItemFactory diskFileItemFactory) throws ServletException { // TODO: this function doesn't set any history data try { // just pass back the binary data InputStreamRequestEntity ire = new InputStreamRequestEntity(httpServletRequest.getInputStream()); postMethodProxyRequest.setRequestEntity(ire); postMethodProxyRequest.setRequestHeader(STRING_CONTENT_TYPE_HEADER_NAME, httpServletRequest.getHeader(STRING_CONTENT_TYPE_HEADER_NAME)); } catch (Exception e) { throw new ServletException(e); } }
protected static void handleRequestData(EntityEnclosingMethod method, Object data) { if (method == null) { throw new IllegalArgumentException("Invalid method, cannot be null"); } if (data != null) { RequestEntity re = null; if (data.getClass().isAssignableFrom(InputStream.class)) { re = new InputStreamRequestEntity((InputStream) data, CONTENT_TYPE_UTF8); } else if (data.getClass().isAssignableFrom(byte[].class)) { re = new ByteArrayRequestEntity((byte[]) data, CONTENT_TYPE_UTF8); } else if (data.getClass().isAssignableFrom(File.class)) { re = new FileRequestEntity((File) data, CONTENT_TYPE_UTF8); } else { // handle as a string try { re = new StringRequestEntity(data.toString(), "text/xml", ENCODING_UTF8); } catch (UnsupportedEncodingException e) { throw new HttpIOException("Encoding data using UTF8 failed :: " + e.getMessage(), e); } } method.setRequestEntity(re); } }
@Override public ClientHttpResponse executeInternal(HttpHeaders headers, byte[] output) throws IOException { for (Map.Entry<String, List<String>> entry : headers.entrySet()) { String headerName = entry.getKey(); for (String headerValue : entry.getValue()) { httpMethod.addRequestHeader(headerName, headerValue); } } if (this.httpMethod instanceof EntityEnclosingMethod) { EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) this.httpMethod; RequestEntity requestEntity = new ByteArrayRequestEntity(output); entityEnclosingMethod.setRequestEntity(requestEntity); } this.httpClient.executeMethod(this.httpMethod); return new CommonsClientHttpResponse(this.httpMethod); }
private void setRequestEntity(EntityEnclosingMethod request, AbstractQuery<?> query) { if (query.getBody() != null) { try { request.setRequestEntity(new StringRequestEntity(query.getBody(), "text/plain; charset=UTF-8", "UTF-8")); } catch (UnsupportedEncodingException e) { throw new ConnectionException("Unsupported encoding", e); } } }
private void writeCurrencyViaJson(String key, String description, int rank) throws IOException { EntityEnclosingMethod put = new PutMethod(restEndpoint + "/" + key); ObjectNode currency = MAPPER.createObjectNode(); currency.put(TYPE, getEntityName()); currency.put("description", description); currency.put("rank", rank); put.setRequestEntity(new StringRequestEntity(currency.toString(), "application/json", "UTF-8")); restClient.executeMethod(put); System.out.println(put.getResponseBodyAsString()); assertEquals(put.getStatusCode(), HttpStatus.SC_OK); }
void write() throws Exception { EntityEnclosingMethod post = new PostMethod(getEndpoint(this.cacheName) + "/" + this.key); if (this.keyContentType != null) { post.addRequestHeader("Key-Content-Type", this.keyContentType); } if (this.value instanceof byte[]) { String contentType = this.contentType == null ? APPLICATION_OCTET_STREAM_TYPE : this.contentType; post.setRequestEntity(new ByteArrayRequestEntity((byte[]) this.value, contentType)); } else { String payload = this.value.toString(); String contentType = this.contentType == null ? TEXT_PLAIN_TYPE : this.contentType; post.setRequestEntity(new StringRequestEntity(payload, contentType, UTF_8.toString())); } restClient.executeMethod(post); assertEquals(post.getStatusCode(), HttpStatus.SC_OK); }
protected HttpMethod createEntityMethod(MuleEvent event, Object body, EntityEnclosingMethod postMethod) throws TransformerException { HttpMethod httpMethod; if (body instanceof String) { httpMethod = (HttpMethod) sendTransformer.transform(body.toString()); } else if (body instanceof byte[]) { byte[] buffer = event.transformMessage(DataType.BYTE_ARRAY_DATA_TYPE); postMethod.setRequestEntity(new ByteArrayRequestEntity(buffer, event.getEncoding())); httpMethod = postMethod; } else { if (!(body instanceof OutputHandler)) { body = event.transformMessage(DataTypeFactory.create(OutputHandler.class)); } OutputHandler outputHandler = (OutputHandler) body; postMethod.setRequestEntity(new StreamPayloadRequestEntity(outputHandler, event)); postMethod.setContentChunked(true); httpMethod = postMethod; } return httpMethod; }
public void testCustomObjectRestPutHotRodEmbeddedGet() throws Exception { final String key = "77"; Person p = new Person("Iker"); // 1. Put with Rest EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); ByteArrayOutputStream bout = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(bout)) { oos.writeObject(p); } put.setRequestHeader("Content-Type", "application/x-java-serialized-object"); put.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(bout.toByteArray()))); cacheFactory.getRestClient().executeMethod(put); // 2. Get with Hot Rod RemoteCache<String, Object> remote = cacheFactory.getHotRodCache(); assertEquals(p, remote.get(key)); // 3. Get with Embedded assertEquals(p, cacheFactory.getEmbeddedCache().getAdvancedCache().withEncoding(IdentityEncoder.class, JavaSerializationEncoder.class).get(key)); }
public void testRestPutEmbeddedHotRodGet() throws Exception { final String key = "1"; // 1. Put with REST EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity( "<hey>ho</hey>".getBytes(), MediaType.TEXT_PLAIN_TYPE)); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded assertEquals("<hey>ho</hey>", cacheFactory.getEmbeddedCache().get(key)); // 3. Get with Hot Rod assertEquals("<hey>ho</hey>", cacheFactory.getHotRodCache().get(key)); }
public void testRestPutEmbeddedHotRodGet() throws Exception { final String key = "1"; // 1. Put with REST EntityEnclosingMethod put = new PutMethod(cacheFactory1.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity( "<hey>ho</hey>".getBytes(), MediaType.TEXT_PLAIN_TYPE)); HttpClient restClient = cacheFactory1.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded Cache embeddedCache = cacheFactory2.getEmbeddedCache().getAdvancedCache(); assertEquals("<hey>ho</hey>", embeddedCache.get(key)); // 3. Get with Hot Rod assertEquals("<hey>ho</hey>", cacheFactory2.getHotRodCache().get(key)); }
private void setEntity(HttlRequest request, EntityEnclosingMethod into) throws IOException { HttlBody body = request.getBody(); if (body != null) { RequestEntity entity; switch (body.getType()) { case MARSHALL: entity = new MarshallableEntity(request, request.getSender().getMarshaller()); break; case STRING: entity = new StringRequestEntity((String) body.getPayload(), null, request.getCharset()); break; case BYTES: entity = new ByteArrayRequestEntity((byte[]) body.getPayload()); break; case STREAM: entity = new InputStreamRequestEntity((InputStream) body.getPayload()); break; case READER: entity = new InputStreamRequestEntity(new ReaderInputStream((Reader) body.getPayload())); break; default: throw new IllegalStateException("Unsupported HttlBody type: " + body.getType()); } into.setRequestEntity(entity); } }
public void testRestPutEmbeddedMemcachedHotRodGetTest() throws Exception { final String key = "3"; // 1. Put with REST EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity( "<hey>ho</hey>".getBytes(), MediaType.TEXT_PLAIN_TYPE)); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded (given a marshaller, it can unmarshall the result) assertEquals("<hey>ho</hey>", cacheFactory.getEmbeddedCache().get(key)); // 3. Get with Memcached (given a marshaller, it can unmarshall the result) assertEquals("<hey>ho</hey>", cacheFactory.getMemcachedClient().get(key)); // 4. Get with Hot Rod (given a marshaller, it can unmarshall the result) assertEquals("<hey>ho</hey>", cacheFactory.getHotRodCache().get(key)); }
public void testRestPutStringHotRodGet() throws Exception { final String key = "1"; // 1. Put text content with REST EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new StringRequestEntity("<hey>ho</hey>", "text/plain", "UTF-8")); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 3. Get with Hot Rod assertEquals("<hey>ho</hey>", cacheFactory.getHotRodCache().get(key)); final String newKey = "2"; final String newValue = "<let's>go</let's>"; //4. Put text content with Hot Rod RemoteCache<String, Object> hotRodCache = cacheFactory.getHotRodCache(); hotRodCache.put(newKey, newValue); //5. Read with rest HttpMethod get = new GetMethod(cacheFactory.getRestUrl() + "/" + newKey); cacheFactory.getRestClient().executeMethod(get); assertEquals(HttpStatus.SC_OK, get.getStatusCode()); assertEquals(newValue, get.getResponseBodyAsString()); }
@Override protected RemoteCacheManager createRemoteCacheManager() throws IOException { RemoteCacheManager remoteCacheManager = new RemoteCacheManager(new org.infinispan.client.hotrod.configuration.ConfigurationBuilder() .addServer().host("localhost").port(hotRodServer.getPort()) .marshaller(new ProtoStreamMarshaller()) .build()); //initialize client-side serialization context SerializationContext serializationContext = ProtoStreamMarshaller.getSerializationContext(remoteCacheManager); ProtoSchemaBuilder protoSchemaBuilder = new ProtoSchemaBuilder(); String protoFile = protoSchemaBuilder.fileName("crypto.proto") .addClass(CryptoCurrency.class) .build(serializationContext); //initialize server-side serialization context via rest endpoint String metadataCacheEndpoint = String.format("http://localhost:%s/rest/%s", restServer.getPort(), PROTOBUF_METADATA_CACHE_NAME); EntityEnclosingMethod put = new PutMethod(metadataCacheEndpoint + "/crypto.proto"); put.setRequestEntity(new StringRequestEntity(protoFile, "text/plain", "UTF-8")); restClient.executeMethod(put); assertEquals(put.getStatusCode(), HttpStatus.SC_OK); return remoteCacheManager; }
@Test public void testRestPutEmbeddedMemcachedHotRodGetTest() throws Exception { final String key = "3"; final Object value = "<hey>ho</hey>"; final Marshaller marshaller = cacheFactory.getMarshaller(); // 1. Put with REST byte[] bytes = marshaller.objectToByteBuffer(value); EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity(bytes, marshaller.mediaType().toString())); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded (given a marshaller, it can unmarshall the result) assertEquals(value, cacheFactory.getEmbeddedCache().get(key)); // 3. Get with Memcached (given a marshaller, it can unmarshall the result) assertEquals(value, cacheFactory.getMemcachedClient().get(key)); // 4. Get with Hot Rod assertEquals(value, cacheFactory.getHotRodCache().get(key)); } }