Java Code Examples for org.apache.commons.httpclient.methods.EntityEnclosingMethod#getResponseBodyAsStream()

The following examples show how to use org.apache.commons.httpclient.methods.EntityEnclosingMethod#getResponseBodyAsStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientEntry.java    From rome with Apache License 2.0 5 votes vote down vote up
/**
 * Update entry by posting new representation of entry to server. Note that you should not
 * attempt to update entries that you get from iterating over a collection they may be "partial"
 * entries. If you want to update an entry, you must get it via one of the
 * <code>getEntry()</code> methods in {@link com.rometools.rome.propono.atom.common.Collection}
 * or {@link com.rometools.rome.propono.atom.common.AtomService}.
 *
 * @throws ProponoException If entry is a "partial" entry.
 */
public void update() throws ProponoException {
    if (partial) {
        throw new ProponoException("ERROR: attempt to update partial entry");
    }
    final EntityEnclosingMethod method = new PutMethod(getEditURI());
    addAuthentication(method);
    final StringWriter sw = new StringWriter();
    final int code = -1;
    try {
        Atom10Generator.serializeEntry(this, sw);
        method.setRequestEntity(new StringRequestEntity(sw.toString(), null, null));
        method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8");
        getHttpClient().executeMethod(method);
        final InputStream is = method.getResponseBodyAsStream();
        if (method.getStatusCode() != 200 && method.getStatusCode() != 201) {
            throw new ProponoException("ERROR HTTP status=" + method.getStatusCode() + " : " + Utilities.streamToString(is));
        }

    } catch (final Exception e) {
        final String msg = "ERROR: updating entry, HTTP code: " + code;
        LOG.debug(msg, e);
        throw new ProponoException(msg, e);
    } finally {
        method.releaseConnection();
    }
}
 
Example 2
Source File: ClientEntry.java    From rome with Apache License 2.0 5 votes vote down vote up
void addToCollection(final ClientCollection col) throws ProponoException {
    setCollection(col);
    final EntityEnclosingMethod method = new PostMethod(getCollection().getHrefResolved());
    addAuthentication(method);
    final StringWriter sw = new StringWriter();
    int code = -1;
    try {
        Atom10Generator.serializeEntry(this, sw);
        method.setRequestEntity(new StringRequestEntity(sw.toString(), null, null));
        method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8");
        getHttpClient().executeMethod(method);
        final InputStream is = method.getResponseBodyAsStream();
        code = method.getStatusCode();
        if (code != 200 && code != 201) {
            throw new ProponoException("ERROR HTTP status=" + code + " : " + Utilities.streamToString(is));
        }
        final Entry romeEntry = Atom10Parser.parseEntry(new InputStreamReader(is), getCollection().getHrefResolved(), Locale.US);
        BeanUtils.copyProperties(this, romeEntry);

    } catch (final Exception e) {
        final String msg = "ERROR: saving entry, HTTP code: " + code;
        LOG.debug(msg, e);
        throw new ProponoException(msg, e);
    } finally {
        method.releaseConnection();
    }
    final Header locationHeader = method.getResponseHeader("Location");
    if (locationHeader == null) {
        LOG.warn("WARNING added entry, but no location header returned");
    } else if (getEditURI() == null) {
        final List<Link> links = getOtherLinks();
        final Link link = new Link();
        link.setHref(locationHeader.getValue());
        link.setRel("edit");
        links.add(link);
        setOtherLinks(links);
    }
}
 
Example 3
Source File: ClientMediaEntry.java    From rome with Apache License 2.0 4 votes vote down vote up
/** Package access, to be called by DefaultClientCollection */
@Override
void addToCollection(final ClientCollection col) throws ProponoException {
    setCollection(col);
    final EntityEnclosingMethod method = new PostMethod(col.getHrefResolved());
    getCollection().addAuthentication(method);
    try {
        final Content c = getContents().get(0);
        if (inputStream != null) {
            method.setRequestEntity(new InputStreamRequestEntity(inputStream));
        } else {
            method.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(getBytes())));
        }
        method.setRequestHeader("Content-type", c.getType());
        method.setRequestHeader("Title", getTitle());
        method.setRequestHeader("Slug", getSlug());
        getCollection().getHttpClient().executeMethod(method);
        if (inputStream != null) {
            inputStream.close();
        }
        final InputStream is = method.getResponseBodyAsStream();
        if (method.getStatusCode() == 200 || method.getStatusCode() == 201) {
            final Entry romeEntry = Atom10Parser.parseEntry(new InputStreamReader(is), col.getHrefResolved(), Locale.US);
            BeanUtils.copyProperties(this, romeEntry);

        } else {
            throw new ProponoException("ERROR HTTP status-code=" + method.getStatusCode() + " status-line: " + method.getStatusLine());
        }
    } catch (final IOException ie) {
        throw new ProponoException("ERROR: saving media entry", ie);
    } catch (final JDOMException je) {
        throw new ProponoException("ERROR: saving media entry", je);
    } catch (final FeedException fe) {
        throw new ProponoException("ERROR: saving media entry", fe);
    } catch (final IllegalAccessException ae) {
        throw new ProponoException("ERROR: saving media entry", ae);
    } catch (final InvocationTargetException te) {
        throw new ProponoException("ERROR: saving media entry", te);
    }
    final Header locationHeader = method.getResponseHeader("Location");
    if (locationHeader == null) {
        LOG.warn("WARNING added entry, but no location header returned");
    } else if (getEditURI() == null) {
        final List<Link> links = getOtherLinks();
        final Link link = new Link();
        link.setHref(locationHeader.getValue());
        link.setRel("edit");
        links.add(link);
        setOtherLinks(links);
    }
}