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:test.TestBase.java

/**
 * Creates an item with a file in the framework.
 * /*  w  w w  .  j a v a2 s . c  om*/
 * @param userHandle The userHandle of a user with the appropriate grants.
 * @return The XML of the created item with a file, given back by the framework.
 * @throws Exception Any exception
 */
protected String createItemWithFile(String userHandle) throws Exception {
    // Prepare the HttpMethod.
    PutMethod method = new PutMethod(ServiceLocator.getFrameworkUrl() + "/st/staging-file");
    method.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(COMPONENT_FILE)));
    method.setRequestHeader("Content-Type", MIME_TYPE);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);

    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    ProxyHelper.executeMethod(client, method);
    logger.debug("Status=" + method.getStatusCode()); // >= HttpServletResponse.SC_MULTIPLE_CHOICE 300 ???
    assertEquals(HttpServletResponse.SC_OK, method.getStatusCode());
    String response = method.getResponseBodyAsString();
    logger.debug("Response=" + response);

    // Create a document from the response.
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(method.getResponseBodyAsStream());
    document.getDocumentElement().normalize();

    // Extract the file information.
    String href = getValue(document, "/staging-file/@href");
    assertNotNull(href);

    // Create an item with the href in the component.
    String item = readFile(ITEM_FILE);
    item = item.replaceFirst("XXX_CONTENT_REF_XXX", ServiceLocator.getFrameworkUrl() + href);
    logger.debug("Item=" + item);
    item = ServiceLocator.getItemHandler(userHandle).create(item);
    assertNotNull(item);
    logger.debug("Item=" + item);

    return item;
}

From source file:uk.ac.ebi.arrayexpress.servlets.GenomeSpaceUploadServlet.java

private Integer createDirectory(String path, DirectoryInfo directoryInfo, String gsToken) throws IOException {
    PutMethod put = new PutMethod(GS_DM_ROOT_URL + GS_ROOT_DIRECTORY + path);
    put.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    put.setRequestHeader("Cookie", "gs-token=" + gsToken);
    put.setRequestHeader("Accept", "application/json");
    put.setRequestHeader("Content-Type", "application/json");
    put.setRequestEntity(new StringRequestEntity("{\"isDirectory\":true}", JSON_MIME_TYPE, "US-ASCII"));
    JSONParser jsonParser = new JSONParser();
    Integer statusCode = null;/*from  w  ww.  j a v a  2s .c  om*/
    try {
        statusCode = httpClient.executeMethod(put);
        if (HttpServletResponse.SC_OK == statusCode) {
            try (InputStream is = put.getResponseBodyAsStream()) {
                directoryInfo
                        .setInfo((JSONObject) jsonParser.parse(new BufferedReader(new InputStreamReader(is))));
            } catch (ParseException x) {
                logger.error("Unable to parse JSON response:", x);
            }
        } else {
            logger.error("Unable create directory, status code [{}]", statusCode);
        }
    } catch (HttpException x) {
        logger.error("Caught an exception:", x);
    } finally {
        put.releaseConnection();
    }

    return statusCode;
}

From source file:uk.ac.ebi.arrayexpress.servlets.GenomeSpaceUploadServlet.java

private Integer putFile(UploadFileInfo fileInfo) throws IOException {
    PutMethod put = new PutMethod(fileInfo.getUploadURL());
    RequestEntity entity = new FileRequestEntity(fileInfo.getFile(), fileInfo.GetContentType());
    put.setRequestEntity(entity);
    put.setRequestHeader("Content-MD5", fileInfo.getContentMD5());

    Integer statusCode = null;/*  w w  w . j  a v  a 2s . c  o  m*/
    try {
        statusCode = httpClient.executeMethod(put);
    } catch (HttpException x) {
        logger.error("Caught an exception:", x);
    } finally {
        put.releaseConnection();
    }
    return statusCode;
}