Search in sources :

Example 1 with StatusLine

use of org.apache.commons.httpclient.StatusLine in project maven-plugins by apache.

the class ClassicJiraDownloader method download.

/**
     * Downloads the given link using the configured HttpClient, possibly following redirects.
     *
     * @param cl the HttpClient
     * @param link the URL to JIRA
     */
private void download(final HttpClient cl, final String link) {
    InputStream in = null;
    OutputStream out = null;
    try {
        GetMethod gm = new GetMethod(link);
        getLog().info("Downloading from JIRA at: " + link);
        gm.setFollowRedirects(true);
        cl.executeMethod(gm);
        StatusLine sl = gm.getStatusLine();
        if (sl == null) {
            getLog().error("Unknown error validating link: " + link);
            return;
        }
        // if we get a redirect, do so
        if (gm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header locationHeader = gm.getResponseHeader("Location");
            if (locationHeader == null) {
                getLog().warn("Site sent redirect, but did not set Location header");
            } else {
                String newLink = locationHeader.getValue();
                getLog().debug("Following redirect to " + newLink);
                download(cl, newLink);
            }
        }
        if (gm.getStatusCode() == HttpStatus.SC_OK) {
            in = gm.getResponseBodyAsStream();
            if (!output.getParentFile().exists()) {
                output.getParentFile().mkdirs();
            }
            // write the response to file
            out = new FileOutputStream(output);
            IOUtil.copy(in, out);
            out.close();
            out = null;
            in.close();
            in = null;
            getLog().debug("Downloading from JIRA was successful");
        } else {
            getLog().warn("Downloading from JIRA failed. Received: [" + gm.getStatusCode() + "]");
        }
    } catch (HttpException e) {
        if (getLog().isDebugEnabled()) {
            getLog().error("Error downloading issues from JIRA:", e);
        } else {
            getLog().error("Error downloading issues from JIRA url: " + e.getLocalizedMessage());
        }
    } catch (IOException e) {
        if (getLog().isDebugEnabled()) {
            getLog().error("Error downloading issues from JIRA:", e);
        } else {
            getLog().error("Error downloading issues from JIRA. Cause is " + e.getLocalizedMessage());
        }
    } finally {
        IOUtil.close(out);
        IOUtil.close(in);
    }
}
Also used : StatusLine(org.apache.commons.httpclient.StatusLine) Header(org.apache.commons.httpclient.Header) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException)

Aggregations

FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Header (org.apache.commons.httpclient.Header)1 HttpException (org.apache.commons.httpclient.HttpException)1 StatusLine (org.apache.commons.httpclient.StatusLine)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1