Example usage for org.apache.commons.httpclient.methods PutMethod setRequestEntity

List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestEntity

Introduction

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

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:au.org.ala.spatial.util.UploadSpatialResource.java

/**
 * sends a PUT or POST call to a URL using authentication and including a
 * file upload/*w  w w .  jav  a 2  s.  c  om*/
 *
 * @param type         one of UploadSpatialResource.PUT for a PUT call or
 *                     UploadSpatialResource.POST for a POST call
 * @param url          URL for PUT/POST call
 * @param username     account username for authentication
 * @param password     account password for authentication
 * @param resourcepath local path to file to upload, null for no file to
 *                     upload
 * @param contenttype  file MIME content type
 * @return server response status code as String or empty String if
 * unsuccessful
 */
public static String httpCall(int type, String url, String username, String password, String resourcepath,
        String contenttype) {
    String output = "";

    HttpClient client = new HttpClient();
    client.setConnectionTimeout(10000);
    client.setTimeout(60000);
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

    RequestEntity entity = null;
    if (resourcepath != null) {
        File input = new File(resourcepath);
        entity = new FileRequestEntity(input, contenttype);
    }

    HttpMethod call = null;
    ;
    if (type == PUT) {
        PutMethod put = new PutMethod(url);
        put.setDoAuthentication(true);
        if (entity != null) {
            put.setRequestEntity(entity);
        }
        call = put;
    } else if (type == POST) {
        PostMethod post = new PostMethod(url);
        if (entity != null) {
            post.setRequestEntity(entity);
        }
        call = post;
    } else {
        //SpatialLogger.log("UploadSpatialResource", "invalid type: " + type);
        return output;
    }

    // Execute the request 
    try {
        int result = client.executeMethod(call);

        output = result + ": " + call.getResponseBodyAsString();
    } catch (Exception e) {
        //SpatialLogger.log("UploadSpatialResource", "failed upload to: " + url);
        output = "0: " + e.getMessage();
        e.printStackTrace(System.out);
    } finally {
        // Release current connection to the connection pool once you are done 
        call.releaseConnection();
    }

    return output;
}

From source file:com.buglabs.dragonfly.util.WSHelper.java

protected static String putBase64(String url, FileInputStream stream) throws HttpException, IOException {
    HttpClient c = new HttpClient();

    PutMethod m = new PutMethod(url);
    byte[] buff = streamToByteArray(stream);

    String em = Base64.encodeBytes(buff);

    m.setRequestEntity(new StringRequestEntity(em));
    c.executeMethod(m);/* w  w  w  . j  av a2 s .  c om*/

    return m.getResponseBodyAsString();
}

From source file:au.org.ala.spatial.util.UploadSpatialResource.java

public static String loadSld(String url, String extra, String username, String password, String resourcepath) {
    System.out.println("loadSld url:" + url);
    System.out.println("path:" + resourcepath);

    String output = "";

    HttpClient client = new HttpClient();
    client.setConnectionTimeout(10000);/*  w  w  w  . ja v  a2  s.co  m*/
    client.setTimeout(60000);

    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    client.getParams().setAuthenticationPreemptive(true);

    File input = new File(resourcepath);

    PutMethod put = new PutMethod(url);
    put.setDoAuthentication(true);

    // Request content will be retrieved directly
    // from the input stream
    RequestEntity entity = new FileRequestEntity(input, "application/vnd.ogc.sld+xml");
    put.setRequestEntity(entity);

    // Execute the request
    try {
        int result = client.executeMethod(put);

        output = result + ": " + put.getResponseBodyAsString();

    } catch (Exception e) {
        e.printStackTrace(System.out);
        output = "0: " + e.getMessage();
    } finally {
        // Release current connection to the connection pool once you are done
        put.releaseConnection();
    }

    return output;
}

From source file:au.org.ala.spatial.util.UploadSpatialResource.java

public static String loadResource(String url, String extra, String username, String password,
        String resourcepath) {/*from ww w .j a v a  2s .c  o  m*/
    String output = "";

    HttpClient client = new HttpClient();
    client.setConnectionTimeout(10000);
    client.setTimeout(60000);

    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

    File input = new File(resourcepath);

    PutMethod put = new PutMethod(url);
    put.setDoAuthentication(true);

    //put.addRequestHeader("Content-type", "application/zip");

    // Request content will be retrieved directly 
    // from the input stream 
    RequestEntity entity = new FileRequestEntity(input, "application/zip");
    put.setRequestEntity(entity);

    // Execute the request 
    try {
        int result = client.executeMethod(put);

        output = result + ": " + put.getResponseBodyAsString();

    } catch (Exception e) {
        e.printStackTrace(System.out);
        output = "0: " + e.getMessage();
    } finally {
        // Release current connection to the connection pool once you are done 
        put.releaseConnection();
    }

    return output;

}

From source file:io.fabric8.gateway.apiman.HTTPGatewayApiManTest.java

public static void configureEngine() throws HttpException, IOException {

    int restPort = httpGatewayServer.getPort() - 1;
    HttpClient httpClient = new HttpClient();

    ObjectMapper mapper = new ObjectMapper();

    Service service = new Service();
    service.setServiceId("HelloWorld");
    service.setEndpoint("http://localhost:18181/root/");
    service.setVersion("1.0");
    service.setOrganizationId("Kurt");

    String serviceJson = mapper.writeValueAsString(service);

    LOG.info("Publishing HelloWorld Service");
    PutMethod method = new PutMethod(
            "http://127.0.0.1:" + restPort + "/rest/apimanager/services/?apikey=apiman-config-key");
    RequestEntity requestEntity = new StringRequestEntity(serviceJson, "application/json", "UTF-8");
    method.setRequestEntity(requestEntity);
    httpClient.executeMethod(method);// ww  w. j  av a2  s.  c  o m

    Application clientApp = new Application();
    clientApp.setApplicationId("clientApp");
    clientApp.setOrganizationId("ClientOrg");
    clientApp.setVersion("1.0");

    Policy blackListPolicy = new Policy();
    blackListPolicy.setPolicyJsonConfig("{ \"ipList\" : [ \"127.0.0.1\" ] }");
    blackListPolicy.setPolicyImpl("class:" + IPBlacklistPolicy.class.getName());

    Contract silverContract = new Contract();
    silverContract.setApiKey(silverHelloServiceApiKey);
    silverContract.setServiceId(service.getServiceId());
    silverContract.setServiceOrgId(service.getOrganizationId());
    silverContract.setServiceVersion(service.getVersion());
    silverContract.getPolicies().add(blackListPolicy);
    clientApp.addContract(silverContract);

    Contract goldContract = new Contract();
    goldContract.setApiKey(goldHelloServiceApiKey);
    goldContract.setServiceId(service.getServiceId());
    goldContract.setServiceOrgId(service.getOrganizationId());
    goldContract.setServiceVersion(service.getVersion());
    clientApp.addContract(goldContract);

    String clientAppJson = mapper.writeValueAsString(clientApp);

    LOG.info("Register clientApp Application");
    method = new PutMethod(
            "http://127.0.0.1:" + restPort + "/rest/apimanager/applications/?apikey=apiman-config-key");
    requestEntity = new StringRequestEntity(clientAppJson, "application/json", "UTF-8");
    method.setRequestEntity(requestEntity);
    httpClient.executeMethod(method);

}

From source file:de.mpg.escidoc.services.test.search.TestBase.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * /*from  w  ww.  j  av a 2 s  .c om*/
 * @param filename The file to upload
 * @param mimetype The mimetype of the file
 * @param userHandle The userhandle to use for upload
 * @return The URL of the uploaded file
 * @throws Exception
 */
protected static URL uploadFile(String filename, String mimetype, String userHandle) throws Exception {
    XmlTransforming xmlTransforming = (XmlTransforming) getService(
            "ejb:search_ear/common_logic/XmlTransformingBean!" + XmlTransforming.class.getName());
    // Prepare the HttpMethod.
    String fwUrl = ServiceLocator.getFrameworkUrl();
    PutMethod method = new PutMethod(fwUrl + "/st/staging-file");

    method.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(filename)));
    method.setRequestHeader("Content-Type", mimetype);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);

    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    ProxyHelper.executeMethod(client, method);
    String response = method.getResponseBodyAsString();
    assertEquals(HttpServletResponse.SC_OK, method.getStatusCode());

    return xmlTransforming.transformUploadResponseToFileURL(response);

}

From source file:com.mycompany.neo4jprotein.neoStart.java

static public void addProperty(String nodeURI, String propertyName, String propertyValue) {
    String output = null;//from  ww  w . j  ava  2 s.  c  o m

    try {
        String nodePointUrl = nodeURI + "/properties/" + propertyName;
        HttpClient client = new HttpClient();
        PutMethod mPut = new PutMethod(nodePointUrl);

        /**
         * set headers
         */
        Header mtHeader = new Header();
        mtHeader.setName("content-type");
        mtHeader.setValue("application/json");
        mtHeader.setName("accept");
        mtHeader.setValue("application/json");
        mPut.addRequestHeader(mtHeader);

        /**
         * set json payload
         */
        String jsonString = "" + propertyValue + "";
        StringRequestEntity requestEntity = new StringRequestEntity(jsonString, "application/json", "UTF-8");
        mPut.setRequestEntity(requestEntity);
        int satus = client.executeMethod(mPut);
        output = mPut.getResponseBodyAsString();

        mPut.releaseConnection();
        System.out.println("satus : " + satus);
        System.out.println("output : " + output);
    } catch (Exception e) {
        System.out.println("Exception in creating node in neo4j : " + e);
    }

}

From source file:au.org.ala.spatial.util.UploadSpatialResource.java

public static String assignSld(String url, String extra, String username, String password, String data) {
    System.out.println("assignSld url:" + url);
    System.out.println("data:" + data);

    String output = "";

    HttpClient client = new HttpClient();
    client.setConnectionTimeout(10000);/*from   w w  w  .  j  a  v a 2  s .  co  m*/
    client.setTimeout(60000);

    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

    PutMethod put = new PutMethod(url);
    put.setDoAuthentication(true);

    // Execute the request
    try {
        // Request content will be retrieved directly
        // from the input stream
        File file = File.createTempFile("sld", "xml");
        System.out.println("file:" + file.getPath());
        FileWriter fw = new FileWriter(file);
        fw.append(data);
        fw.close();
        RequestEntity entity = new FileRequestEntity(file, "text/xml");
        put.setRequestEntity(entity);

        int result = client.executeMethod(put);

        output = result + ": " + put.getResponseBodyAsString();

    } catch (Exception e) {
        output = "0: " + e.getMessage();
        e.printStackTrace(System.out);
    } finally {
        // Release current connection to the connection pool once you are done
        put.releaseConnection();
    }

    return output;
}

From source file:at.ac.tuwien.auto.sewoa.http.HttpRetrieverImpl.java

public byte[] putData(String url, String data) {
    byte[] result = new byte[] {};
    HttpClient httpClient = httpClientFactory.createClient();
    PutMethod putmethod = new PutMethod(url);
    putmethod.setRequestEntity(new StringRequestEntity(data));

    HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams();
    params.setConnectionTimeout((int) TimeUnit.SECONDS.toMillis(CONNECTION_TO));
    params.setSoTimeout((int) TimeUnit.SECONDS.toMillis(SOCKET_TO));
    putmethod.addRequestHeader("Content-Type", "application/xml");
    putmethod.addRequestHeader("Accept", "application/xml");

    try {/*from  w ww . j  a v a 2 s.  co  m*/
        result = send(httpClient, putmethod);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.sixdimensions.wcm.cq.dao.webdav.WebDavDAO.java

/**
 * Uploads the file into CQ./*from  w  w  w. j av a2s  .co m*/
 * 
 * @param file
 *            The file to upload
 * @param path
 *            the path to which to upload the file
 * @throws IOException
 */
public void uploadFile(final File file, final String path) throws IOException {
    this.log.debug("uploadFile");
    final String url = this.config.getHost() + ":" + this.config.getPort() + path + "/" + file.getName();
    final PutMethod put = new PutMethod(url);

    put.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file)));
    final int status = this.httpClient.executeMethod(put);
    if ((status != 200) && (status != 201) && (status != 204)) {
        throw new IOException("uploadFile(" + url + ") failed, status code=" + status);
    }
}