Search in sources :

Example 11 with StringEntity

use of org.apache.http.entity.StringEntity in project camel by apache.

the class CxfRsConsumerTest method testPutConsumer.

@Test
public void testPutConsumer() throws Exception {
    HttpPut put = new HttpPut("http://localhost:" + CXT + "/rest/customerservice/customers");
    StringEntity entity = new StringEntity(PUT_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    put.addHeader("test", "header1;header2");
    put.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 12 with StringEntity

use of org.apache.http.entity.StringEntity in project camel by apache.

the class CxfRsConsumerWithBeanTest method sendPutRequest.

private void sendPutRequest(String uri) throws Exception {
    HttpPut put = new HttpPut(uri);
    StringEntity entity = new StringEntity("string");
    entity.setContentType("text/plain");
    put.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("c20string", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut)

Example 13 with StringEntity

use of org.apache.http.entity.StringEntity in project camel by apache.

the class HipchatProducer method post.

protected StatusLine post(String urlPath, Map<String, String> postParam) throws IOException {
    HttpPost httpPost = new HttpPost(getConfig().hipChatUrl() + urlPath);
    httpPost.setEntity(new StringEntity(MAPPER.writeValueAsString(postParam), ContentType.APPLICATION_JSON));
    CloseableHttpResponse closeableHttpResponse = HTTP_CLIENT.execute(httpPost);
    try {
        return closeableHttpResponse.getStatusLine();
    } finally {
        closeableHttpResponse.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 14 with StringEntity

use of org.apache.http.entity.StringEntity in project hadoop by apache.

the class WebAppProxyServlet method proxyLink.

/**
   * Download link and have it be the response.
   * @param req the http request
   * @param resp the http response
   * @param link the link to download
   * @param c the cookie to set if any
   * @param proxyHost the proxy host
   * @param method the http method
   * @throws IOException on any error.
   */
private static void proxyLink(final HttpServletRequest req, final HttpServletResponse resp, final URI link, final Cookie c, final String proxyHost, final HTTP method) throws IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY).setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    // Make sure we send the request from the proxy address in the config
    // since that is what the AM filter checks against. IP aliasing or
    // similar could cause issues otherwise.
    InetAddress localAddress = InetAddress.getByName(proxyHost);
    if (LOG.isDebugEnabled()) {
        LOG.debug("local InetAddress for proxy host: {}", localAddress);
    }
    client.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
    HttpRequestBase base = null;
    if (method.equals(HTTP.GET)) {
        base = new HttpGet(link);
    } else if (method.equals(HTTP.PUT)) {
        base = new HttpPut(link);
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        ((HttpPut) base).setEntity(new StringEntity(sb.toString()));
    } else {
        resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    @SuppressWarnings("unchecked") Enumeration<String> names = req.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        if (PASS_THROUGH_HEADERS.contains(name)) {
            String value = req.getHeader(name);
            if (LOG.isDebugEnabled()) {
                LOG.debug("REQ HEADER: {} : {}", name, value);
            }
            base.setHeader(name, value);
        }
    }
    String user = req.getRemoteUser();
    if (user != null && !user.isEmpty()) {
        base.setHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
    }
    OutputStream out = resp.getOutputStream();
    try {
        HttpResponse httpResp = client.execute(base);
        resp.setStatus(httpResp.getStatusLine().getStatusCode());
        for (Header header : httpResp.getAllHeaders()) {
            resp.setHeader(header.getName(), header.getValue());
        }
        if (c != null) {
            resp.addCookie(c);
        }
        InputStream in = httpResp.getEntity().getContent();
        if (in != null) {
            IOUtils.copyBytes(in, out, 4096, true);
        }
    } finally {
        base.releaseConnection();
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) InputStreamReader(java.io.InputStreamReader) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) BufferedReader(java.io.BufferedReader) InetAddress(java.net.InetAddress)

Example 15 with StringEntity

use of org.apache.http.entity.StringEntity in project hive by apache.

the class JIRAService method publishComments.

void publishComments(String comments) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {
        String url = String.format("%s/rest/api/2/issue/%s/comment", mUrl, mName);
        URL apiURL = new URL(mUrl);
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(apiURL.getHost(), apiURL.getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(mUser, mPassword));
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute("preemptive-auth", new BasicScheme());
        httpClient.addRequestInterceptor(new PreemptiveAuth(), 0);
        HttpPost request = new HttpPost(url);
        ObjectMapper mapper = new ObjectMapper();
        StringEntity params = new StringEntity(mapper.writeValueAsString(new Body(comments)));
        request.addHeader("Content-Type", "application/json");
        request.setEntity(params);
        HttpResponse httpResponse = httpClient.execute(request, localcontext);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 201) {
            throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        mLogger.info("JIRA Response Metadata: " + httpResponse);
    } catch (Exception e) {
        mLogger.error("Encountered error attempting to post comment to " + mName, e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpPost(org.apache.http.client.methods.HttpPost) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) URL(java.net.URL) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) StatusLine(org.apache.http.StatusLine) StringEntity(org.apache.http.entity.StringEntity) AuthScope(org.apache.http.auth.AuthScope) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

StringEntity (org.apache.http.entity.StringEntity)409 HttpPost (org.apache.http.client.methods.HttpPost)223 HttpResponse (org.apache.http.HttpResponse)136 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)129 HttpPut (org.apache.http.client.methods.HttpPut)89 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)88 Test (org.junit.Test)84 IOException (java.io.IOException)78 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)60 HttpEntity (org.apache.http.HttpEntity)55 JsonNode (com.fasterxml.jackson.databind.JsonNode)54 Deployment (org.activiti.engine.test.Deployment)50 TestHttpClient (io.undertow.testutils.TestHttpClient)30 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)27 StatusLine (org.apache.http.StatusLine)26 HttpGet (org.apache.http.client.methods.HttpGet)25 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)23 Task (org.activiti.engine.task.Task)21 HttpRequest (org.apache.http.HttpRequest)21 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)20