Example usage for org.apache.http.client.methods HttpPost METHOD_NAME

List of usage examples for org.apache.http.client.methods HttpPost METHOD_NAME

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPost METHOD_NAME.

Prototype

String METHOD_NAME

To view the source code for org.apache.http.client.methods HttpPost METHOD_NAME.

Click Source Link

Usage

From source file:org.apache.edgent.connectors.http.runtime.HttpRequester.java

@Override
public R apply(T t) {

    if (client == null)
        client = clientCreator.get();/*from ww  w  . ja  va2 s .  c om*/

    String m = method.apply(t);
    String uri = url.apply(t);
    HttpUriRequest request;

    switch (m) {

    case HttpGet.METHOD_NAME:
        request = new HttpGet(uri);
        break;
    case HttpDelete.METHOD_NAME:
        request = new HttpDelete(uri);
        break;
    case HttpPost.METHOD_NAME:
        request = new HttpPost(uri);
        break;
    case HttpPut.METHOD_NAME:
        request = new HttpPut(uri);
        break;

    default:
        throw new IllegalArgumentException();
    }

    // If entity is not null means http request should have a body
    if (entity != null) {

        HttpEntity body = entity.apply(t);

        if (request instanceof HttpEntityEnclosingRequest == false) {
            throw new IllegalArgumentException("Http request does not support body");
        }

        ((HttpEntityEnclosingRequest) request).setEntity(body);
    }

    try {
        try (CloseableHttpResponse response = client.execute(request)) {
            return responseProcessor.apply(t, response);
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.edgent.test.connectors.http.HttpTest.java

@Test
public void testPost() throws Exception {
    DirectProvider ep = new DirectProvider();

    Topology topology = ep.newTopology();

    String url = "http://httpbin.org/post";

    TStream<String> stream = topology.strings(url);
    TStream<String> rc = HttpStreams.<String, String>requestsWithBody(stream, HttpClients::noAuthentication,
            t -> HttpPost.METHOD_NAME, t -> t, t -> new ByteArrayEntity(t.getBytes()),
            HttpResponders.inputOn200());

    Tester tester = topology.getTester();

    Condition<List<String>> endCondition = tester.streamContents(rc, url);

    tester.complete(ep, new JsonObject(), endCondition, 10, TimeUnit.SECONDS);

    assertTrue(endCondition.valid());/*from   ww  w . ja v  a2 s.  c  o  m*/
}

From source file:com.github.matthesrieke.jprox.JProxViaParameterServlet.java

private HttpUriRequest prepareRequest(HttpServletRequest req, String target) throws IOException {
    String method = req.getMethod();
    if (method.equals(HttpGet.METHOD_NAME)) {
        return new HttpGet(target);
    } else if (method.equals(HttpPost.METHOD_NAME)) {
        HttpPost post = new HttpPost(target);
        post.setEntity(new ByteArrayEntity(readInputStream(req.getInputStream(), req.getContentLength()),
                ContentType.parse(req.getContentType())));
        return post;
    }//from  ww  w.  j  a v a 2s .c o m
    throw new UnsupportedOperationException("Only GET and POST are supported by this proxy.");
}

From source file:org.deviceconnect.message.http.impl.factory.AbstractHttpMessageFactory.java

/**
 * HTTP 1???dConnect??.//  ww  w . java 2s. co m
 * @param message HTTP
 * @return dConnect
 */
protected DConnectMessage parseFirstLine(final M message) {
    mLogger.entering(getClass().getName(), "parseFirstLine", message);
    DConnectMessage dmessage = null;

    if (message instanceof HttpRequest) {

        // put action
        String method = ((HttpRequest) message).getRequestLine().getMethod();
        mLogger.fine("HTTP request method: " + method);

        DConnectRequestMessage drequest = new DConnectRequestMessage();
        if (HttpGet.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_GET);
        } else if (HttpPut.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_PUT);
        } else if (HttpPost.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_POST);
        } else if (HttpDelete.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_DELETE);
        } else {
            throw new IllegalArgumentException("invalid http request mehtod: " + method);
        }

        dmessage = drequest;
    } else if (message instanceof HttpResponse) {
        dmessage = new DConnectResponseMessage();
    } else {
        throw new IllegalArgumentException(
                "unkown http message class instance: " + message.getClass().getName());
    }

    mLogger.exiting(getClass().getName(), "parseFirstLine", dmessage);
    return dmessage;
}

From source file:org.camunda.connect.httpclient.impl.AbstractHttpConnector.java

@SuppressWarnings("unchecked")
protected <T extends HttpRequestBase> T createHttpRequestBase(Q request) {
    String url = request.getUrl();
    if (url != null && !url.trim().isEmpty()) {
        String method = request.getMethod();
        if (HttpGet.METHOD_NAME.equals(method)) {
            return (T) new HttpGet(url);
        } else if (HttpPost.METHOD_NAME.equals(method)) {
            return (T) new HttpPost(url);
        } else if (HttpPut.METHOD_NAME.equals(method)) {
            return (T) new HttpPut(url);
        } else if (HttpDelete.METHOD_NAME.equals(method)) {
            return (T) new HttpDelete(url);
        } else if (HttpPatch.METHOD_NAME.equals(method)) {
            return (T) new HttpPatch(url);
        } else if (HttpHead.METHOD_NAME.equals(method)) {
            return (T) new HttpHead(url);
        } else if (HttpOptions.METHOD_NAME.equals(method)) {
            return (T) new HttpOptions(url);
        } else if (HttpTrace.METHOD_NAME.equals(method)) {
            return (T) new HttpTrace(url);
        } else {/*from w w w .ja  v a2  s . co m*/
            throw LOG.unknownHttpMethod(method);
        }
    } else {
        throw LOG.requestUrlRequired();
    }
}

From source file:org.camunda.connect.httpclient.impl.AbstractHttpRequest.java

public Q post() {
    return method(HttpPost.METHOD_NAME);
}

From source file:org.xmetdb.rest.protocol.attachments.CallableAttachmentImporter.java

protected RemoteTask remoteImport(DBAttachment target) throws Exception {
    Reference uri = new Reference(queryService);

    HttpClient client = createHTTPClient(uri.getHostDomain(), uri.getHostPort());
    RemoteTask task = new RemoteTask(client, new URL(String.format("%s/dataset", queryService)),
            "text/uri-list", createPOSTEntity(attachment), HttpPost.METHOD_NAME);

    try {// w w w .j  a  va 2  s. co  m
        task = wait(task, System.currentTimeMillis());
        String dataset_uri = "dataset_uri";
        URL dataset = task.getResult();
        if (task.isCompletedOK()) {
            if (!"text/uri-list".equals(attachment.getFormat())) { //was a file
                //now post the dataset uri to get the /R datasets (query table)
                attachment.setFormat("text/uri-list");
                attachment.setDescription(dataset.toExternalForm());
                task = new RemoteTask(client, new URL(String.format("%s/dataset", queryService)),
                        "text/uri-list", createPOSTEntity(attachment), HttpPost.METHOD_NAME);
                task = wait(task, System.currentTimeMillis());
            }

            Form form = new Form();
            form.add(dataset_uri, dataset.toURI().toString());
            for (String algorithm : algorithms) { //just launch tasks and don't wait
                List<NameValuePair> formparams = new ArrayList<NameValuePair>();
                formparams.add(new BasicNameValuePair(dataset_uri, dataset.toURI().toString()));
                HttpEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
                HttpClient newclient = createHTTPClient(uri.getHostDomain(), uri.getHostPort());
                try {
                    new RemoteTask(newclient, new URL(String.format("%s%s", queryService, algorithm)),
                            "text/uri-list", entity, HttpPost.METHOD_NAME);

                } catch (Exception x) {
                } finally {
                    try {
                        newclient.getConnectionManager().shutdown();
                    } catch (Exception x) {
                    }
                }
            }
        }

    } catch (Exception x) {
        task.setError(new ResourceException(Status.SERVER_ERROR_BAD_GATEWAY,
                String.format("Error importing chemical structures dataset to %s", uri), x));
    } finally {
        try {
            client.getConnectionManager().shutdown();
        } catch (Exception x) {
        }
    }
    return task;
}

From source file:com.blacklocus.jres.request.index.JresIndexDocument.java

@Override
public String getHttpMethod() {
    return id == null ? HttpPost.METHOD_NAME : HttpPut.METHOD_NAME;
}

From source file:com.googlecode.webutilities.servlets.WebProxyServlet.java

private HttpUriRequest getRequest(String method, String url) {
    if (HttpPost.METHOD_NAME.equals(method)) {
        return new HttpPost(url);
    } else if (HttpPut.METHOD_NAME.equals(method)) {
        return new HttpPut(url);
    } else if (HttpDelete.METHOD_NAME.equals(method)) {
        return new HttpDelete(url);
    } else if (HttpOptions.METHOD_NAME.equals(method)) {
        return new HttpOptions(url);
    } else if (HttpHead.METHOD_NAME.equals(method)) {
        return new HttpHead(url);
    }/*from  ww  w . j  a v  a  2 s . co  m*/
    return new HttpGet(url);
}