List of usage examples for org.apache.http.client.methods HttpEntityEnclosingRequestBase setEntity
public void setEntity(final HttpEntity entity)
From source file:org.olat.restapi.RestConnection.java
public void addEntity(HttpEntityEnclosingRequestBase put, NameValuePair... pairs) throws UnsupportedEncodingException { if (pairs == null || pairs.length == 0) return;//from w w w.j a v a 2s .com List<NameValuePair> pairList = new ArrayList<NameValuePair>(); for (NameValuePair pair : pairs) { pairList.add(pair); } HttpEntity myEntity = new UrlEncodedFormEntity(pairList, "UTF-8"); put.setEntity(myEntity); }
From source file:org.olat.restapi.RestConnection.java
/** * Add an object (application/json)/*from w ww . java 2 s.com*/ * @param put * @param obj * @throws UnsupportedEncodingException */ public void addJsonEntity(HttpEntityEnclosingRequestBase put, Object obj) throws UnsupportedEncodingException { if (obj == null) return; String objectStr = stringuified(obj); HttpEntity myEntity = new StringEntity(objectStr, ContentType.APPLICATION_JSON); put.setEntity(myEntity); }
From source file:org.olat.restapi.RestConnection.java
public void addMultipart(HttpEntityEnclosingRequestBase post, String filename, File file) throws UnsupportedEncodingException { HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addTextBody("filename", filename) .addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM, filename).build(); post.setEntity(entity); }
From source file:org.opennms.netmgt.ncs.northbounder.NCSNorthbounder.java
private void postAlarms(HttpEntity entity) { //Need a configuration bean for these int connectionTimeout = 3000; int socketTimeout = 3000; Integer retryCount = 3;//from www. j a va 2 s. com HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion()); URI uri = m_config.getURI(); System.err.println("uri = " + uri); final HttpClientWrapper clientWrapper = HttpClientWrapper.create().setSocketTimeout(socketTimeout) .setConnectionTimeout(connectionTimeout).setRetries(retryCount).useBrowserCompatibleCookies() .dontReuseConnections(); if ("https".equals(uri.getScheme())) { try { clientWrapper.useRelaxedSSL("https"); } catch (final GeneralSecurityException e) { throw new NorthbounderException("Failed to configure Relaxed SSL handling.", e); } } final HttpEntityEnclosingRequestBase method = m_config.getMethod().getRequestMethod(uri); if (m_config.getVirtualHost() != null && !m_config.getVirtualHost().trim().isEmpty()) { method.setHeader(HTTP.TARGET_HOST, m_config.getVirtualHost()); } if (m_config.getUserAgent() != null && !m_config.getUserAgent().trim().isEmpty()) { method.setHeader(HTTP.USER_AGENT, m_config.getUserAgent()); } method.setProtocolVersion(httpVersion); method.setEntity(entity); CloseableHttpResponse response = null; try { System.err.println("execute: " + method); response = clientWrapper.execute(method); } catch (ClientProtocolException e) { throw new NorthbounderException(e); } catch (IOException e) { throw new NorthbounderException(e); } finally { IOUtils.closeQuietly(clientWrapper); } if (response != null) { try { int code = response.getStatusLine().getStatusCode(); final HttpResponseRange range = new HttpResponseRange("200-399"); if (!range.contains(code)) { LOG.warn("response code out of range for uri: {}. Expected {} but received {}", uri, range, code); throw new NorthbounderException("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code); } } finally { IOUtils.closeQuietly(clientWrapper); } } LOG.debug(response != null ? response.getStatusLine().getReasonPhrase() : "Response was null"); }
From source file:org.openrepose.nodeservice.httpcomponent.HttpComponentRequestProcessor.java
/** * Process an entity enclosing http method. These methods can handle a request body. * * @param method/*w w w . j a v a 2s . co m*/ * @return * @throws IOException */ // todo: remove the need for a synchronized method -- this is the only method that needs to be synchronized for now // since it modifies the sourceRequest public synchronized HttpRequestBase process(HttpEntityEnclosingRequestBase method) throws IOException { final int contentLength = getEntityLength(); setHeaders(method); method.setEntity(new InputStreamEntity(sourceRequest.getInputStream(), contentLength)); return method; }
From source file:org.rapidoid.http.HttpClient.java
public Future<byte[]> request(String verb, String uri, Map<String, String> headers, Map<String, String> data, Map<String, String> files, byte[] body, String contentType, Callback<byte[]> callback, boolean fullResponse) { headers = U.safe(headers);/*w w w . j a v a 2 s . com*/ data = U.safe(data); files = U.safe(files); HttpRequestBase req; boolean canHaveBody = false; if ("GET".equalsIgnoreCase(verb)) { req = new HttpGet(uri); } else if ("DELETE".equalsIgnoreCase(verb)) { req = new HttpDelete(uri); } else if ("OPTIONS".equalsIgnoreCase(verb)) { req = new HttpOptions(uri); } else if ("HEAD".equalsIgnoreCase(verb)) { req = new HttpHead(uri); } else if ("TRACE".equalsIgnoreCase(verb)) { req = new HttpTrace(uri); } else if ("POST".equalsIgnoreCase(verb)) { req = new HttpPost(uri); canHaveBody = true; } else if ("PUT".equalsIgnoreCase(verb)) { req = new HttpPut(uri); canHaveBody = true; } else if ("PATCH".equalsIgnoreCase(verb)) { req = new HttpPatch(uri); canHaveBody = true; } else { throw U.illegalArg("Illegal HTTP verb: " + verb); } for (Entry<String, String> e : headers.entrySet()) { req.addHeader(e.getKey(), e.getValue()); } if (canHaveBody) { HttpEntityEnclosingRequestBase entityEnclosingReq = (HttpEntityEnclosingRequestBase) req; if (body != null) { NByteArrayEntity entity = new NByteArrayEntity(body); if (contentType != null) { entity.setContentType(contentType); } entityEnclosingReq.setEntity(entity); } else { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Entry<String, String> entry : files.entrySet()) { String filename = entry.getValue(); File file = IO.file(filename); builder = builder.addBinaryBody(entry.getKey(), file, ContentType.DEFAULT_BINARY, filename); } for (Entry<String, String> entry : data.entrySet()) { builder = builder.addTextBody(entry.getKey(), entry.getValue(), ContentType.DEFAULT_TEXT); } ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { builder.build().writeTo(stream); } catch (IOException e) { throw U.rte(e); } byte[] bytes = stream.toByteArray(); NByteArrayEntity entity = new NByteArrayEntity(bytes, ContentType.MULTIPART_FORM_DATA); entityEnclosingReq.setEntity(entity); } } Log.debug("Starting HTTP request", "request", req.getRequestLine()); return execute(client, req, callback, fullResponse); }