eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType));
addContentLengthRequestHeader(state, conn); if (getRequestHeader("Content-Type") == null) { RequestEntity requestEntity = getRequestEntity(); if (requestEntity != null && requestEntity.getContentType() != null) { setRequestHeader("Content-Type", requestEntity.getContentType());
entityMethod.setRequestEntity(new BytesArrayRequestEntity(ba)); entityMethod.setContentChunked(false);
+ "HttpState, HttpConnection)"); if ((getRequestHeader("content-length") == null) && (getRequestHeader("Transfer-Encoding") == null)) { long len = getRequestContentLength(); if (len < 0) { if (getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1)) { addRequestHeader("Transfer-Encoding", "chunked"); } else { throw new ProtocolException(getEffectiveVersion() + " does not support chunk encoding"); addRequestHeader("Content-Length", String.valueOf(len));
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); }
try { Atom10Generator.serializeEntry(this, sw); method.setRequestEntity(new StringRequestEntity(sw.toString(), null, null)); method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8"); getHttpClient().executeMethod(method); final InputStream is = method.getResponseBodyAsStream(); if (method.getStatusCode() != 200 && method.getStatusCode() != 201) { throw new ProponoException("ERROR HTTP status=" + method.getStatusCode() + " : " + Utilities.streamToString(is)); throw new ProponoException(msg, e); } finally { method.releaseConnection();
method.getParams().setContentCharset(webRequestSettings.getCharset()); method.setQueryString(queryString); if (webRequestSettings.getRequestBody() != null) { final String body = webRequestSettings.getRequestBody(); final String charset = webRequestSettings.getCharset(); method.setRequestEntity(new StringRequestEntity(body, null, charset)); method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final String contentType = webRequestSettings.getAdditionalHeaders().get("Content-type"); final String charset = webRequestSettings.getCharset(); method.setRequestEntity(new StringRequestEntity(body, contentType, charset));
entityEnclosingMethod.getParams() ); entityEnclosingMethod.setRequestEntity(multipartRequestEntity); entityEnclosingMethod.setRequestHeader(STRING_CONTENT_TYPE_HEADER_NAME, multipartRequestEntity.getContentType()); } catch (FileUploadException fileUploadException) { throw new ServletException(fileUploadException);
method.setRequestEntity(new InputStreamRequestEntity(inputStream)); } else { method.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(getBytes()))); method.setRequestHeader("Content-type", updateContent.getType()); } else if (getEditURI() != null) { final StringWriter sw = new StringWriter(); Atom10Generator.serializeEntry(this, sw); method.setRequestEntity(new StringRequestEntity(sw.toString(), null, null)); method.setRequestHeader("Content-type", "application/atom+xml; charset=utf8"); } else { throw new ProponoException("ERROR: media entry has no edit URI or media-link URI"); method.addRequestHeader("Title", getTitle()); getCollection().getHttpClient().executeMethod(method); if (inputStream != null) { inputStream.close(); final InputStream is = method.getResponseBodyAsStream(); if (method.getStatusCode() != 200 && method.getStatusCode() != 201) { throw new ProponoException("ERROR HTTP status=" + method.getStatusCode() + " : " + Utilities.streamToString(is)); throw new ProponoException("ERROR: saving media entry"); if (method.getStatusCode() != 201) { throw new ProponoException("ERROR HTTP status=" + method.getStatusCode());
/** * 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); } }
final Content c = getContents().get(0); if (inputStream != null) { method.setRequestEntity(new InputStreamRequestEntity(inputStream)); } else { method.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(getBytes()))); method.setRequestHeader("Content-type", c.getType()); method.setRequestHeader("Title", getTitle()); method.setRequestHeader("Slug", getSlug()); getCollection().getHttpClient().executeMethod(method); if (inputStream != null) { inputStream.close(); final InputStream is = method.getResponseBodyAsStream(); if (method.getStatusCode() == 200 || method.getStatusCode() == 201) { final Entry romeEntry = Atom10Parser.parseEntry(new InputStreamReader(is), col.getHrefResolved(), Locale.US); BeanUtils.copyProperties(this, romeEntry); throw new ProponoException("ERROR HTTP status-code=" + method.getStatusCode() + " status-line: " + method.getStatusLine()); throw new ProponoException("ERROR: saving media entry", te); final Header locationHeader = method.getResponseHeader("Location"); if (locationHeader == null) { LOG.warn("WARNING added entry, but no location header returned");
httpMethod.setRequestEntity(requestEntity); case HttpURLConnection.HTTP_ACCEPTED: if(LOGGER.isDebugEnabled()) LOGGER.debug("HTTP "+ httpMethod.getStatusText() + " <-- " + url); if(ignoreResponseContentOnSuccess) return ""; String response = IOUtils.toString(httpMethod.getResponseBodyAsStream()); return response; default: String badresponse = IOUtils.toString(httpMethod.getResponseBodyAsStream()); String message = getGeoNetworkErrorMessage(badresponse); + " " + httpMethod.getStatusText() + " -- " + httpMethod.getName() + " " +url + " : " } finally { if(httpMethod != null) httpMethod.releaseConnection();
post.getParams().setContentCharset(StandardCharsets.ISO_8859_1.name()); if (request.getByteData() != null) { post.setRequestEntity(new ByteArrayRequestEntity(request.getByteData())); post.setRequestHeader("Content-Length", String.valueOf(request.getByteData().length)); } else if (request.getStringData() != null) { post.setRequestEntity(new StringRequestEntity(request.getStringData(), "text/xml", bodyCharset)); post.setRequestHeader("Content-Length", String.valueOf(request.getStringData().getBytes(bodyCharset).length)); } else if (request.getStreamData() != null) { InputStreamRequestEntity r = new InputStreamRequestEntity(request.getStreamData()); post.setRequestEntity(r); post.setRequestHeader("Content-Length", String.valueOf(r.getContentLength())); post.setRequestHeader("Content-Length", String.valueOf(sb.length())); post.setRequestEntity(new StringRequestEntity(sb.toString(), "text/xml", StandardCharsets.ISO_8859_1.name())); post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); MultipartRequestEntity mre = createMultipartRequestEntity(bodyCharset, request.getParts(), post.getParams()); post.setRequestEntity(mre); post.setRequestHeader("Content-Type", mre.getContentType()); post.setRequestHeader("Content-Length", String.valueOf(mre.getContentLength())); } else if (request.getFile() != null) { File file = request.getFile(); post.setRequestHeader("Content-Length", String.valueOf(file.length())); post.setRequestEntity(r); post.setRequestHeader("Content-Length", String.valueOf(r.getContentLength())); } finally {
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); }
@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; }
private String sendRasterRequest(String target, String request, String method, String username, String password) throws HttpException, IOException { HttpClient client = new HttpClient(); EntityEnclosingMethod requestMethod = null; if (method.equalsIgnoreCase("POST")) { requestMethod = new PostMethod(target); requestMethod.setRequestHeader("Content-type", "application/xml"); } if (method.equalsIgnoreCase("PUT")) { requestMethod = new PutMethod(target); requestMethod.setRequestHeader("Content-type", "text/plain"); } requestMethod.setRequestBody(request); UsernamePasswordCredentials creds = new UsernamePasswordCredentials( username, password); client.getState().setCredentials(null, null, creds); int statusCode = client.executeMethod(requestMethod); if (!((statusCode == HttpStatus.SC_OK) || (statusCode == HttpStatus.SC_CREATED))) { System.err.println("Method failed: " + requestMethod.getStatusLine()); } // Read the response body. byte[] responseBody = requestMethod.getResponseBody(); return new String(responseBody); }
private void checkForContentType(MuleMessage msg, EntityEnclosingMethod method) { // if a content type was specified on the endpoint, use it String outgoingContentType = msg.getInvocationProperty(HttpConstants.HEADER_CONTENT_TYPE); if (outgoingContentType != null) { method.setRequestHeader(HttpConstants.HEADER_CONTENT_TYPE, outgoingContentType); } }
if (httpMethod instanceof EntityEnclosingMethod) ((EntityEnclosingMethod)httpMethod).setContentChunked(context.getContentChunked());
private RequestEntity configureMultipartFileUpload(HttpMethod m, final RestRequest request, RequestEntity requestEntity, Map<String, RestMultipart> multipartFiles) { MultipartRequestEntity multipartRequestEntity = null; // Current File Name reading for tracking missing file String fileName = null; List<Part> fileParts = new ArrayList<Part>(multipartFiles.size()); // Read File Part for (Map.Entry<String, RestMultipart> multipartFile : multipartFiles.entrySet()) { Part filePart = createMultipart(multipartFile.getKey(), multipartFile.getValue()); fileParts.add(filePart); } Part[] parts = fileParts.toArray(new Part[fileParts.size()]); multipartRequestEntity = new MultipartRequestEntity(parts, ((EntityEnclosingMethod) m).getParams()); return multipartRequestEntity; }
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)); }