Example usage for org.apache.commons.httpclient.methods InputStreamRequestEntity InputStreamRequestEntity

List of usage examples for org.apache.commons.httpclient.methods InputStreamRequestEntity InputStreamRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods InputStreamRequestEntity InputStreamRequestEntity.

Prototype

public InputStreamRequestEntity(InputStream paramInputStream) 

Source Link

Usage

From source file:org.exoplatform.tqa.template.SenderWebdav.java

/**
 * Create a WEBDAV file by sending current file
 * @param client// w w w  .ja  v  a  2s  . c  om
 * @param pathRESTWithFolder
 * @param fileLocal
 * @throws HttpException
 * @throws IOException
 */
static void createFile(HttpClient client, String pathRESTWithFolder, File fileLocal)
        throws HttpException, IOException {
    String fileWEBDAV = pathRESTWithFolder + "/" + fileLocal.getName();

    PutMethod method = new PutMethod(fileWEBDAV);
    RequestEntity requestEntity = new InputStreamRequestEntity(new FileInputStream(fileLocal));
    method.setRequestEntity(requestEntity);
    client.executeMethod(method);
    //     LOGGER.info("CREATE file: " + fileWEBDAV);
}

From source file:org.fao.geonet.services.publisher.GeoServerRest.java

/**
 * //from   w ww  .  ja  va2s . c o  m
 * @param method
 *            e.g. 'POST', 'GET', 'PUT' or 'DELETE'
 * @param urlParams
 *            REST API parameter
 * @param postData
 *            XML data
 * @param file
 *            File to upload
 * @param contentType
 *            type of content in case of post data or file updload.
 * @param saveResponse
 * @return
 * @throws IOException
 */
public int sendREST(String method, String urlParams, String postData, File file, String contentType,
        Boolean saveResponse) throws IOException {

    response = "";
    final HttpClient c = httpClientFactory.newHttpClient();
    String url = this.restUrl + urlParams;
    if (Log.isDebugEnabled(LOGGER_NAME)) {
        Log.debug(LOGGER_NAME, "url:" + url);
        Log.debug(LOGGER_NAME, "method:" + method);
        Log.debug(LOGGER_NAME, "postData:" + postData);
    }

    HttpMethod m;
    if (method.equals(METHOD_PUT)) {
        m = new PutMethod(url);
        if (file != null) {
            ((PutMethod) m).setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file)));
        }
        if (postData != null) {
            ((PutMethod) m).setRequestEntity(new StringRequestEntity(postData, contentType, "UTF-8"));
        }
    } else if (method.equals(METHOD_DELETE)) {
        m = new DeleteMethod(url);
    } else if (method.equals(METHOD_POST)) {
        m = new PostMethod(url);
        if (postData != null) {
            ((PostMethod) m).setRequestEntity(new StringRequestEntity(postData, contentType, "UTF-8"));
        }

    } else {
        m = new GetMethod(url);
    }

    if (contentType != null && !"".equals(contentType)) {
        m.setRequestHeader("Content-type", contentType);
    }

    m.setDoAuthentication(true);

    status = c.executeMethod(m);
    if (Log.isDebugEnabled(LOGGER_NAME))
        Log.debug(LOGGER_NAME, "status:" + status);
    if (saveResponse)
        this.response = m.getResponseBodyAsString();

    return status;
}

From source file:org.geosdi.geoplatform.services.httpclient.GeoSDIHttpClient.java

/**
 * @param url/*from   w w w  .  j  a v  a 2s  .  c  om*/
 * @param postContent
 * @param postContentType
 * @return {@link HTTPResponse}
 * @throws IOException
 */
public HTTPResponse post(URL url, InputStream postContent, String postContentType) throws IOException {
    PostMethod postMethod = new PostMethod(url.toExternalForm());
    LOGGER.info("Inject OpenAM Cookie");
    if (this.headers != null) {
        List<String> values = this.headers.stream()
                .map(value -> String.join("=", value.getHeaderKey(), value.getHeaderValue())).collect(toList());
        postMethod.setRequestHeader("Cookie", String.join(";", values));
    }

    RequestEntity requestEntity = new InputStreamRequestEntity(postContent);
    postMethod.setRequestEntity(requestEntity);
    int responseCode = this.executeMethod(postMethod);
    if (200 != responseCode) {
        postMethod.releaseConnection();
        throw new IOException(
                "Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm());
    } else {
        return new GeoSDIHttpClient.HttpMethodResponse(postMethod);
    }
}

From source file:org.geotools.data.ows.MultithreadedHttpClient.java

@Override
public HTTPResponse post(final URL url, final InputStream postContent, final String postContentType)
        throws IOException {

    PostMethod postMethod = new PostMethod(url.toExternalForm());
    postMethod.setDoAuthentication(user != null && password != null);
    if (tryGzip) {
        postMethod.setRequestHeader("Accept-Encoding", "gzip");
    }//from w  w  w.  jav  a  2s.c  om
    if (postContentType != null) {
        postMethod.setRequestHeader("Content-type", postContentType);
    }
    RequestEntity requestEntity = new InputStreamRequestEntity(postContent);
    postMethod.setRequestEntity(requestEntity);

    int responseCode = client.executeMethod(postMethod);
    if (200 != responseCode) {
        postMethod.releaseConnection();
        throw new IOException(
                "Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm());
    }

    return new HttpMethodResponse(postMethod);
}

From source file:org.geotools.data.wfs.impl.MultithreadedHttpClient.java

@Override
public HTTPResponse post(final URL url, final InputStream postContent, final String postContentType)
        throws IOException {

    PostMethod postMethod = new PostMethod(url.toExternalForm());
    postMethod.setDoAuthentication(user != null && password != null);
    if (postContentType != null) {
        postMethod.setRequestHeader("Content-type", postContentType);
    }/*from  w  w w . ja  v a 2  s  . co m*/
    RequestEntity requestEntity = new InputStreamRequestEntity(postContent);
    postMethod.setRequestEntity(requestEntity);

    int responseCode = client.executeMethod(postMethod);
    if (200 != responseCode) {
        postMethod.releaseConnection();
        throw new IOException(
                "Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm());
    }

    return new HttpMethodResponse(postMethod);
}

From source file:org.httpobjects.proxy.Proxy.java

protected void setRequestRepresentation(Request req, EntityEnclosingMethod method) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    req.representation().write(out);/*from w  ww . j av  a  2  s  .com*/
    method.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(out.toByteArray())));
}

From source file:org.hydracache.client.http.HttpHydraCacheClient.java

@Override
public void put(Object key, Data data) {
    Identity identity = nodePartition.get(key);
    String uri = constructUri(key, identity);

    try {//ww w .  j  a v  a  2  s.  c om
        PutMethod putMethod = new PutMethod(uri);
        Buffer buffer = Buffer.allocate();
        protocolEncoder.encode(new BlobDataMessage(data), buffer.asDataOutpuStream());

        RequestEntity requestEntity = new InputStreamRequestEntity(buffer.asDataInputStream());
        putMethod.setRequestEntity(requestEntity);

        validateResponseCode(httpClient.executeMethod(putMethod));
    } catch (IOException ioe) {
        logger.error("Cannot write to connection.", ioe);
    }
}

From source file:org.hydracache.client.transport.HttpTransport.java

/**
 * @param path/*from w  w  w  .j  a va  2s.c o  m*/
 * @return
 */
private HttpMethod getMethod(RequestMessage requestMessage) {
    String action = requestMessage.getMethod();

    HttpMethod method = null;
    String uri = httpClient.getHostConfiguration().getHostURL() + "/" + requestMessage.getPath();

    if (action.equals("put")) {

        method = new PutMethod(uri);
        Buffer buffer = (Buffer) requestMessage.getRequestData();
        RequestEntity requestEntity = new InputStreamRequestEntity(buffer.asDataInputStream());

        ((PutMethod) method).setRequestEntity(requestEntity);
    } else {
        method = new GetMethod(uri);
    }
    return method;

}

From source file:org.infoscoop.request.ProxyRequest.java

public int executePost() throws Exception {
    PostMethod method = null;//from w w w  .  ja v a  2s .co m
    try {
        HttpClient client = this.newHttpClient();
        method = new PostMethod(this.getTargetURL());
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(1, false));
        if (this.getRequestBody() != null)
            method.setRequestEntity(new InputStreamRequestEntity(this.getRequestBody()));
        ProxyFilterContainer filterContainer = (ProxyFilterContainer) SpringUtil.getBean(filterType);
        return filterContainer.invoke(client, method, this);
    } catch (ProxyAuthenticationException e) {
        if (e.isTraceOn()) {
            log.error(this.getTargetURL());
            log.error("", e);
        } else {
            log.warn(this.getTargetURL() + " : " + e.getMessage());
        }
        return 401;
    }
}

From source file:org.infoscoop.request.ProxyRequest.java

public int executePut() throws Exception {
    PutMethod method = null;//from   www  . jav  a  2  s .c o  m
    try {
        HttpClient client = this.newHttpClient();
        method = new PutMethod(this.getTargetURL());
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(1, false));
        if (this.getRequestBody() != null)
            method.setRequestEntity(new InputStreamRequestEntity(this.getRequestBody()));
        ProxyFilterContainer filterContainer = (ProxyFilterContainer) SpringUtil.getBean(filterType);
        return filterContainer.invoke(client, method, this);
    } catch (ProxyAuthenticationException e) {
        if (e.isTraceOn()) {
            log.error(this.getTargetURL());
            log.error("", e);
        } else {
            log.warn(this.getTargetURL() + " : " + e.getMessage());
        }
        return 401;
    }
}