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:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java

public void updateLearningObjectInstanceUserReportCommentForCollaboration(
        LearningObjectInstanceUserReportCommentOnComment reportComment, int instanceId, int learningObjectId,
        int collaborationId) throws Exception {
    String uri = String.format(_baseUri
            + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Collaborations/%s/Report/comments",
            learningObjectId, instanceId, collaborationId);
    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 {// w ww.j a  v  a 2s  .  c  o 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 updateLearningObjectInstanceUserReportForCollaboration(LearningObjectInstanceUserReport userReport,
        int learningObjectId, int instanceId, int collaborationId) throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Collaborations/%s/Report",
            learningObjectId, instanceId, collaborationId);
    PutMethod method = (PutMethod) getInitializedHttpMethod(_httpClient, uri, HttpMethodType.PUT);
    String userReportAsXml = serializeLearningObjectInstanceUserReportToXML(userReport);
    InputStream is = new ByteArrayInputStream(userReportAsXml.getBytes("UTF-8"));
    method.setRequestEntity(new InputStreamRequestEntity(is));
    try {//from   ww w . j a  v a2 s.  c  om
        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 updateLearningObjectInstance(LearningObjectInstance instance, int instanceId, int learningObjectId)
        throws Exception {
    String uri = String.format(_baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s",
            learningObjectId, instanceId);
    PutMethod method = (PutMethod) getInitializedHttpMethod(_httpClient, uri, HttpMethodType.PUT);
    String loiAsXml = serializeLearningObjectInstanceToXML(instance);
    InputStream is = new ByteArrayInputStream(loiAsXml.getBytes("UTF-8"));
    method.setRequestEntity(new InputStreamRequestEntity(is));
    try {/*from   w  w  w.  j  a va 2s  . c  o 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:fr.cls.atoll.motu.processor.wps.framework.WPSFactory.java

/**
 * Post async./*from   w  w w.  j ava 2 s.com*/
 * 
 * @param url the url
 * @param postBody the post body
 * @param headers the headers
 * 
 * @return the int
 * 
 * @throws HttpException the http exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static int postAsync(String url, InputStream postBody, Map<String, String> headers)
        throws HttpException, IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("postAsync(String, InputStream, Map<String,String>) - entering");
    }

    // TODO no proxies used
    HttpClient client = new HttpClient();
    HttpMethodParams httpMethodParams = new HttpMethodParams();
    httpMethodParams.setIntParameter(HttpMethodParams.SO_TIMEOUT, 1);

    PostMethod post = new PostMethod(url);
    post.setRequestEntity(new InputStreamRequestEntity(postBody));
    post.setParams(httpMethodParams);

    for (String key : headers.keySet()) {
        post.setRequestHeader(key, headers.get(key));
    }
    int retcode = -1;
    try {
        retcode = client.executeMethod(post);
    } catch (SocketTimeoutException e) {
        LOG.error("postAsync(String, InputStream, Map<String,String>)", e);

        // do nothing
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("postAsync(String, InputStream, Map<String,String>) - exiting");
    }
    return retcode;
}

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

public void updateLearningObjectiveUserAssessments(int learningObjectId, int instanceId, int[] userIds,
        List<LearningObjectiveAssessment> assessments) throws Exception {
    String uri = String.format(_baseUri
            + "/LearningObjectService.svc/learningObjects/%s/instances/%s/LearningObjectiveUserAssessments",
            learningObjectId, instanceId);
    QueryStringBuilder query = new QueryStringBuilder(uri, false);
    if (userIds != null && userIds.length > 0) {
        query.AddParameter("userIds", intArrayToCsvString(userIds));
    }//from   w w  w .j ava  2  s  . c o m
    PutMethod method = (PutMethod) getInitializedHttpMethod(_httpClient, query.getQueryString(),
            HttpMethodType.PUT);

    String assessmentsAsXml = serializeLearningObjectiveAssessmentsToXml(assessments);
    InputStream is = new ByteArrayInputStream(assessmentsAsXml.getBytes("UTF-8"));
    method.setRequestEntity(new InputStreamRequestEntity(is));
    try {
        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 updateLearningObjectiveCollaborationAssessments(int learningObjectId, int instanceId,
        int collaborationId, List<LearningObjectiveAssessment> assessments) throws Exception {
    String uri = String.format(_baseUri
            + "/LearningObjectService.svc/learningObjects/%s/instances/%s/LearningObjectiveCollaborationAssessments/%s",
            learningObjectId, instanceId, collaborationId);
    PutMethod method = (PutMethod) getInitializedHttpMethod(_httpClient, uri, HttpMethodType.PUT);

    String assessmentsAsXml = serializeLearningObjectiveAssessmentsToXml(assessments);
    InputStream is = new ByteArrayInputStream(assessmentsAsXml.getBytes("UTF-8"));
    method.setRequestEntity(new InputStreamRequestEntity(is));
    try {/*  w  w w.  ja  v a  2s.  c  o 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:nl.nn.adapterframework.http.HttpSender.java

protected void addMtomMultiPartToPostMethod(PostMethod hmethod, String message, ParameterValueList parameters,
        ParameterResolutionContext prc) throws SenderException, MessagingException, IOException {
    MyMimeMultipart mimeMultipart = new MyMimeMultipart("related");
    String start = null;//from ww w  .  j  a va 2s.  co m
    if (StringUtils.isNotEmpty(getInputMessageParam())) {
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setContent(message, "application/xop+xml; charset=UTF-8; type=\"text/xml\"");
        start = "<" + getInputMessageParam() + ">";
        mimeBodyPart.setContentID(start);
        ;
        mimeMultipart.addBodyPart(mimeBodyPart);
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "appended (string)part [" + getInputMessageParam() + "] with value ["
                    + message + "]");
    }
    if (parameters != null) {
        for (int i = 0; i < parameters.size(); i++) {
            ParameterValue pv = parameters.getParameterValue(i);
            String paramType = pv.getDefinition().getType();
            String name = pv.getDefinition().getName();
            if (Parameter.TYPE_INPUTSTREAM.equals(paramType)) {
                Object value = pv.getValue();
                if (value instanceof FileInputStream) {
                    FileInputStream fis = (FileInputStream) value;
                    String fileName = null;
                    String sessionKey = pv.getDefinition().getSessionKey();
                    if (sessionKey != null) {
                        fileName = (String) prc.getSession().get(sessionKey + "Name");
                    }
                    MimeBodyPart mimeBodyPart = new PreencodedMimeBodyPart("binary");
                    mimeBodyPart.setDisposition(javax.mail.Part.ATTACHMENT);
                    ByteArrayDataSource ds = new ByteArrayDataSource(fis, "application/octet-stream");
                    mimeBodyPart.setDataHandler(new DataHandler(ds));
                    mimeBodyPart.setFileName(fileName);
                    mimeBodyPart.setContentID("<" + name + ">");
                    mimeMultipart.addBodyPart(mimeBodyPart);
                    if (log.isDebugEnabled())
                        log.debug(getLogPrefix() + "appended (file)part [" + name + "] with value [" + value
                                + "] and name [" + fileName + "]");
                } else {
                    throw new SenderException(getLogPrefix() + "unknown inputstream [" + value.getClass()
                            + "] for parameter [" + name + "]");
                }
            } else {
                String value = pv.asStringValue("");
                MimeBodyPart mimeBodyPart = new MimeBodyPart();
                mimeBodyPart.setContent(value, "text/xml");
                if (start == null) {
                    start = "<" + name + ">";
                    mimeBodyPart.setContentID(start);
                } else {
                    mimeBodyPart.setContentID("<" + name + ">");
                }
                mimeMultipart.addBodyPart(mimeBodyPart);
                if (log.isDebugEnabled())
                    log.debug(
                            getLogPrefix() + "appended (string)part [" + name + "] with value [" + value + "]");
            }
        }
    }
    if (StringUtils.isNotEmpty(getMultipartXmlSessionKey())) {
        String multipartXml = (String) prc.getSession().get(getMultipartXmlSessionKey());
        if (StringUtils.isEmpty(multipartXml)) {
            log.warn(getLogPrefix() + "sessionKey [" + getMultipartXmlSessionKey() + "] is empty");
        } else {
            Element partsElement;
            try {
                partsElement = XmlUtils.buildElement(multipartXml);
            } catch (DomBuilderException e) {
                throw new SenderException(getLogPrefix() + "error building multipart xml", e);
            }
            Collection parts = XmlUtils.getChildTags(partsElement, "part");
            if (parts == null || parts.size() == 0) {
                log.warn(getLogPrefix() + "no part(s) in multipart xml [" + multipartXml + "]");
            } else {
                int c = 0;
                Iterator iter = parts.iterator();
                while (iter.hasNext()) {
                    c++;
                    Element partElement = (Element) iter.next();
                    //String partType = partElement.getAttribute("type");
                    String partName = partElement.getAttribute("name");
                    String partMimeType = partElement.getAttribute("mimeType");
                    String partSessionKey = partElement.getAttribute("sessionKey");
                    Object partObject = prc.getSession().get(partSessionKey);
                    if (partObject instanceof FileInputStream) {
                        FileInputStream fis = (FileInputStream) partObject;
                        MimeBodyPart mimeBodyPart = new PreencodedMimeBodyPart("binary");
                        mimeBodyPart.setDisposition(javax.mail.Part.ATTACHMENT);
                        ByteArrayDataSource ds = new ByteArrayDataSource(fis,
                                (partMimeType == null ? "application/octet-stream" : partMimeType));
                        mimeBodyPart.setDataHandler(new DataHandler(ds));
                        mimeBodyPart.setFileName(partName);
                        mimeBodyPart.setContentID("<" + partName + ">");
                        mimeMultipart.addBodyPart(mimeBodyPart);
                        if (log.isDebugEnabled())
                            log.debug(getLogPrefix() + "appended (file)part [" + partSessionKey
                                    + "]  with value [" + partObject + "] and name [" + partName + "]");
                    } else {
                        String partValue = (String) prc.getSession().get(partSessionKey);
                        MimeBodyPart mimeBodyPart = new MimeBodyPart();
                        mimeBodyPart.setContent(partValue, "text/xml");
                        if (start == null) {
                            start = "<" + partName + ">";
                            mimeBodyPart.setContentID(start);
                        } else {
                            mimeBodyPart.setContentID("<" + partName + ">");
                        }
                        mimeMultipart.addBodyPart(mimeBodyPart);
                        if (log.isDebugEnabled())
                            log.debug(getLogPrefix() + "appended (string)part [" + partSessionKey
                                    + "]  with value [" + partValue + "]");
                    }
                }
            }
        }
    }
    MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
    mimeMessage.setContent(mimeMultipart);
    mimeMessage.saveChanges();
    InputStreamRequestEntity request = new InputStreamRequestEntity(mimeMessage.getInputStream());
    hmethod.setRequestEntity(request);
    String contentTypeMtom = "multipart/related; type=\"application/xop+xml\"; start=\"" + start
            + "\"; start-info=\"text/xml\"; boundary=\"" + mimeMultipart.getBoundary() + "\"";
    Header header = new Header("Content-Type", contentTypeMtom);
    hmethod.addRequestHeader(header);
}

From source file:org.alfresco.repo.remoteconnector.RemoteConnectorRequestImpl.java

public void setRequestBody(InputStream body) {
    requestBody = new InputStreamRequestEntity(body);
}

From source file:org.apache.abdera.protocol.client.AbderaClient.java

/**
 * Sends an HTTP POST request to the specified URI.
 * //from   ww  w  .ja  v a 2s. c o m
 * @param uri The request URI
 * @param in An InputStream providing the payload of the request
 * @param options The request options
 */
public ClientResponse post(String uri, InputStream in, RequestOptions options) {
    return execute("POST", uri, new InputStreamRequestEntity(in), options);
}

From source file:org.apache.abdera.protocol.client.AbderaClient.java

/**
 * Sends an HTTP PUT request to the specified URI.
 * /*  w w w.jav a 2 s  .c o  m*/
 * @param uri The request URI
 * @param in An InputStream providing the payload of the request
 * @param options The request options
 */
public ClientResponse put(String uri, InputStream in, RequestOptions options) {
    return execute("PUT", uri, new InputStreamRequestEntity(in), options);
}