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:de.mpg.mpdl.inge.pubman.web.easySubmission.EasySubmission.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * /* w ww.j  a v a  2  s . c  om*/
 * @param uploadedFile 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 If anything goes wrong...
 */
protected URL uploadFile(UploadedFile uploadedFile, String mimetype, String userHandle) throws Exception {
    // Prepare the HttpMethod.
    String fwUrl = PropertyReader.getFrameworkUrl();
    PutMethod method = new PutMethod(fwUrl + "/st/staging-file");
    /*
     * if(uploadedFile.isTempFile()) {
     */
    InputStream fis = uploadedFile.getInputstream();
    method.setRequestEntity(new InputStreamRequestEntity(fis));

    /*
     * } else { method.setRequestEntity(new InputStreamRequestEntity(new
     * ByteArrayInputStream(uploadedFile.getData()))); }
     */
    method.setRequestHeader("Content-Type", mimetype);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    ProxyHelper.setProxy(client, fwUrl);
    client.executeMethod(method);
    String response = method.getResponseBodyAsString();
    fis.close();
    return xmlTransforming.transformUploadResponseToFileURL(response);
}

From source file:de.mpg.mpdl.inge.pubman.web.easySubmission.EasySubmission.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * /*from w ww  .j a  va2  s.  c o  m*/
 * @param InputStream 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 If anything goes wrong...
 */
protected URL uploadFile(InputStream in, String mimetype, String userHandle) throws Exception {
    // Prepare the HttpMethod.
    String fwUrl = PropertyReader.getFrameworkUrl();
    PutMethod method = new PutMethod(fwUrl + "/st/staging-file");
    method.setRequestEntity(new InputStreamRequestEntity(in));
    method.setRequestHeader("Content-Type", mimetype);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    client.executeMethod(method);
    String response = method.getResponseBodyAsString();
    return xmlTransforming.transformUploadResponseToFileURL(response);
}

From source file:de.mpg.escidoc.pubman.easySubmission.EasySubmission.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * //from   www . j  av a  2  s.co  m
 * @param uploadedFile 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 If anything goes wrong...
 */
protected URL uploadFile(UploadedFile uploadedFile, String mimetype, String userHandle) throws Exception {
    // Prepare the HttpMethod.
    String fwUrl = de.mpg.escidoc.services.framework.ServiceLocator.getFrameworkUrl();
    PutMethod method = new PutMethod(fwUrl + "/st/staging-file");
    /*
     if(uploadedFile.isTempFile())
     {
     */
    InputStream fis = uploadedFile.getInputstream();
    method.setRequestEntity(new InputStreamRequestEntity(fis));

    /*    
    }
        else
        {
    method.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(uploadedFile.getData())));
        }
        */
    method.setRequestHeader("Content-Type", mimetype);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    ProxyHelper.setProxy(client, fwUrl);
    client.executeMethod(method);
    String response = method.getResponseBodyAsString();
    fis.close();
    return xmlTransforming.transformUploadResponseToFileURL(response);
}

From source file:de.mpg.escidoc.pubman.easySubmission.EasySubmission.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * //w w  w.j  ava2 s. com
 * @param InputStream 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 If anything goes wrong...
 */
protected URL uploadFile(InputStream in, String mimetype, String userHandle) throws Exception {
    // Prepare the HttpMethod.
    String fwUrl = de.mpg.escidoc.services.framework.ServiceLocator.getFrameworkUrl();
    PutMethod method = new PutMethod(fwUrl + "/st/staging-file");
    method.setRequestEntity(new InputStreamRequestEntity(in));
    method.setRequestHeader("Content-Type", mimetype);
    method.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
    // Execute the method with HttpClient.
    HttpClient client = new HttpClient();
    client.executeMethod(method);
    String response = method.getResponseBodyAsString();
    return xmlTransforming.transformUploadResponseToFileURL(response);
}

From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java

/**
 * Uploads a file to the staging servlet and returns the corresponding URL.
 * //  w w w.j  a va2  s.  c  o  m
 * @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 If anything goes wrong...
 */
protected URL uploadFile(String filename, String mimetype, final String userHandle) throws Exception {
    // Prepare the HttpMethod.
    String fwUrl = PropertyReader.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) getService(XmlTransforming.SERVICE_NAME))
            .transformUploadResponseToFileURL(response);
}

From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java

/**
 * Creates an item with a file in the framework.
 * //  w  w  w.  j  a va  2  s . co  m
 * @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(PropertyReader.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", PropertyReader.getFrameworkUrl() + href);
    logger.debug("Item=" + item);
    item = ServiceLocator.getItemHandler(userHandle).create(item);
    assertNotNull(item);
    logger.debug("Item=" + item);

    return item;
}

From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java

public void sendNotification(Notification notification, int instanceId, int learningObjectId) throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Notification",
            learningObjectId, instanceId);
    PostMethod method = (PostMethod) getInitializedHttpMethod(_httpClient, uri, HttpMethodType.POST);
    String reportAsXml = serializeNotificationToXML(notification);
    InputStream is = new ByteArrayInputStream(reportAsXml.getBytes("UTF-8"));
    method.setRequestEntity(new InputStreamRequestEntity(is));
    try {/* w ww.  ja v a2 s .  c  o  m*/
        int statusCode = _httpClient.executeMethod(method);
        // POST methods, may return 200, 201, 204
        if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED
                && statusCode != HttpStatus.SC_NOT_MODIFIED) {
            throw new HTTPException(statusCode);
        }

    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
}

From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java

public void updateLearningObjectInstanceUserReports(List<LearningObjectInstanceUserReport> userReports,
        int instanceId, int learningObjectId) throws Exception {
    String uri = String.format(_baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Reports",
            learningObjectId, instanceId);
    PostMethod method = (PostMethod) getInitializedHttpMethod(_httpClient, uri, HttpMethodType.POST);
    String reportsAsXml = serializeLearningObjectInstanceUserReportsToXML(userReports);
    InputStream is = new ByteArrayInputStream(reportsAsXml.getBytes("UTF-8"));
    method.setRequestEntity(new InputStreamRequestEntity(is));
    try {/*from   www. j av a 2  s  .  c o m*/
        int statusCode = _httpClient.executeMethod(method);
        // POST methods, may return 200, 201, 204
        if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED
                && statusCode != HttpStatus.SC_NOT_MODIFIED) {
            throw new HTTPException(statusCode);
        }

    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
}

From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java

public void updateLearningObjectInstanceUserReport(LearningObjectInstanceUserReport userReport, int instanceId,
        int learningObjectId, int userId) throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Reports/%s",
            learningObjectId, instanceId, userId);
    PutMethod method = (PutMethod) getInitializedHttpMethod(_httpClient, uri, HttpMethodType.PUT);
    String reportAsXml = serializeLearningObjectInstanceUserReportToXML(userReport);
    InputStream is = new ByteArrayInputStream(reportAsXml.getBytes("UTF-8"));
    method.setRequestEntity(new InputStreamRequestEntity(is));
    try {/*from   w w w .  j  av a  2 s  .  co  m*/
        int statusCode = _httpClient.executeMethod(method);
        // Put methods, may return 200, 201, 204
        if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED
                && statusCode != HttpStatus.SC_NOT_MODIFIED) {
            throw new HTTPException(statusCode);
        }

    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
}

From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java

public void updateLearningObjectInstanceUserReportComment(
        LearningObjectInstanceUserReportCommentOnComment reportComment, int instanceId, int learningObjectId,
        int userId) throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Reports/%s/comments",
            learningObjectId, instanceId, userId);
    PutMethod method = (PutMethod) getInitializedHttpMethod(_httpClient, uri, HttpMethodType.PUT);
    String commentAsXml = serializeLearningObjectInstanceUserReportCommentOnCommentToXML(reportComment);
    InputStream is = new ByteArrayInputStream(commentAsXml.getBytes("UTF-8"));
    method.setRequestEntity(new InputStreamRequestEntity(is));
    try {/*from  w  w  w.  j  a v  a  2 s.com*/
        int statusCode = _httpClient.executeMethod(method);
        // Put methods, may return 200, 201, 204
        if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED
                && statusCode != HttpStatus.SC_NOT_MODIFIED) {
            throw new HTTPException(statusCode);
        }

    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
}