Example usage for org.apache.commons.httpclient HttpMethod releaseConnection

List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod releaseConnection.

Prototype

public abstract void releaseConnection();

Source Link

Usage

From source file:com.panoramagl.downloaders.PLHTTPFileDownloader.java

/**download methods*/

@Override/*from   w ww .j  a va2 s  .  c o m*/
protected byte[] downloadFile() {
    this.setRunning(true);
    byte[] result = null;
    InputStream is = null;
    ByteArrayOutputStream bas = null;
    String url = this.getURL();
    PLFileDownloaderListener listener = this.getListener();
    boolean hasListener = (listener != null);
    int responseCode = -1;
    long startTime = System.currentTimeMillis();
    // HttpClient instance
    HttpClient client = new HttpClient();
    // Method instance
    HttpMethod method = new GetMethod(url);
    // Method parameters
    HttpMethodParams methodParams = method.getParams();
    methodParams.setParameter(HttpMethodParams.USER_AGENT, "PanoramaGL Android");
    methodParams.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
    methodParams.setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(this.getMaxAttempts(), false));
    try {
        // Execute the method
        responseCode = client.executeMethod(method);
        if (responseCode != HttpStatus.SC_OK)
            throw new IOException(method.getStatusText());
        // Get content length
        Header header = method.getRequestHeader("Content-Length");
        long contentLength = (header != null ? Long.parseLong(header.getValue()) : 1);
        if (this.isRunning()) {
            if (hasListener)
                listener.didBeginDownload(url, startTime);
        } else
            throw new PLRequestInvalidatedException(url);
        // Get response body as stream
        is = method.getResponseBodyAsStream();
        bas = new ByteArrayOutputStream();
        byte[] buffer = new byte[256];
        int length = 0, total = 0;
        // Read stream
        while ((length = is.read(buffer)) != -1) {
            if (this.isRunning()) {
                bas.write(buffer, 0, length);
                total += length;
                if (hasListener)
                    listener.didProgressDownload(url, (int) (((float) total / (float) contentLength) * 100.0f));
            } else
                throw new PLRequestInvalidatedException(url);
        }
        if (total == 0)
            throw new IOException("Request data has invalid size (0)");
        // Get data
        if (this.isRunning()) {
            result = bas.toByteArray();
            if (hasListener)
                listener.didEndDownload(url, result, System.currentTimeMillis() - startTime);
        } else
            throw new PLRequestInvalidatedException(url);
    } catch (Throwable e) {
        if (this.isRunning()) {
            PLLog.error("PLHTTPFileDownloader::downloadFile", e);
            if (hasListener)
                listener.didErrorDownload(url, e.toString(), responseCode, result);
        }
    } finally {
        if (bas != null) {
            try {
                bas.close();
            } catch (IOException e) {
                PLLog.error("PLHTTPFileDownloader::downloadFile", e);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                PLLog.error("PLHTTPFileDownloader::downloadFile", e);
            }
        }
        // Release the connection
        method.releaseConnection();
    }
    this.setRunning(false);
    return result;
}

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

public List<LearningObjectInstanceUserReport> getLearningObjectInstanceUserReports(int instanceId,
        int learningObjectId, int pageIndex, int pageSize, LearningObjectInstanceUserReport.OrderBy orderBy,
        OrderDirection orderDirection) throws Exception {
    String uri = String.format(_baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Reports",
            learningObjectId, instanceId);
    uri = AppendPagingParams(uri, pageIndex, pageSize, orderBy, orderDirection);

    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    List<LearningObjectInstanceUserReport> reports = new ArrayList<LearningObjectInstanceUserReport>();
    try {/*from w  w w . j a  v a 2s . c o m*/
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            reports = deserializeXMLToListOfLearningObjectInstanceUserReport(method.getResponseBodyAsStream());
        }

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

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

public List<AssessmentStatus> getPossibleAssessmentStatuses(int instanceId, int learningObjectId)
        throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/PossibleAssessmentStatuses",
            learningObjectId, instanceId);
    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    List<AssessmentStatus> assessmentStatuses = new ArrayList<AssessmentStatus>();
    try {//from w ww.j a  va  2s  .  c om
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            assessmentStatuses = deserializeXMLToListOfPossibleAssessmentStatuses(
                    method.getResponseBodyAsStream());
        }

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

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

public int getLearningObjectInstanceUsersCount(int instanceId, int learningObjectId, int[] userIds,
        boolean includeTeachers) throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Users/count",
            learningObjectId, instanceId);
    uri = appendLearningObjectInstanceUsersExtraParameters(uri, userIds, includeTeachers);

    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    int usersCount = 0;
    try {/*from  ww w.  j  a v a  2s  . co  m*/
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(method.getResponseBodyAsStream());
            usersCount = Integer.parseInt(doc.getRootElement().getText());
        }
    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
    return usersCount;
}

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

public int getLearningObjectInstanceUserReportsCount(int instanceId, int learningObjectId) throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Reports/count",
            learningObjectId, instanceId);
    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    int reportsCount = 0;
    try {//  w  w  w .j a va 2s  .  c om
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(method.getResponseBodyAsStream());
            reportsCount = Integer.parseInt(doc.getRootElement().getText());
        }
    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
    return reportsCount;
}

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

public List<AssessmentStatusItem> getAssessmentStatusItems(int instanceId, int learningObjectId)
        throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/AssessmentStatusItems",
            learningObjectId, instanceId);
    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    List<AssessmentStatusItem> assessmentStatusItems = new ArrayList<AssessmentStatusItem>();
    try {//from w  ww. j ava2 s .  co m
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            assessmentStatusItems = deserializeXMLToListOfAssessmentStatusItems(
                    method.getResponseBodyAsStream());
        }

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

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

/**
 * Get a list of collaborations participants for instance.
 * @param learningObjectId//from www.  j a  va  2s . co  m
 * @param instanceId
 * @param collaborationIds The array of user IDs to filter by.
 * @return
 * @throws java.lang.Exception
 */
public List<CollaborationParticipant> getLearningObjectInstanceCollaborationsParticipants(int learningObjectId,
        int instanceId, int[] collaborationIds) throws Exception {
    String uri = String.format(_baseUri
            + "/LearningObjectService.svc/learningObjects/%s/instances/%s/collaborations/participants?collaborationIds=%s",
            learningObjectId, instanceId, intArrayToCsvString(collaborationIds));

    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    List<CollaborationParticipant> collaborationParticipants = new ArrayList<CollaborationParticipant>();
    try {
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            collaborationParticipants = deserializeXMLToListOfCollaborationParticipant(
                    method.getResponseBodyAsStream());
        }
    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
    return collaborationParticipants;
}

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

public Site getSiteForCurrentUser() throws Exception {
    String uri = String.format(_baseUri + "/LearningObjectService.svc/SiteForCurrentUser");
    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    Site siteForUser = null;//  w ww . ja v  a  2 s  .c  o  m
    try {
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            if (Integer.parseInt(method.getResponseHeader("Content-Length").getValue()) > 0) {
                siteForUser = deserializeXMLToSite(method.getResponseBodyAsStream());
            } else {
                return null;
            }
        }

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

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

/**
 * Returns customer settings/*from   www . j  ava2 s.com*/
 */
public CustomerSettings getCustomerSettings() throws Exception {
    String uri = String.format(_baseUri + "/LearningObjectService.svc/CustomerSettings");
    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    CustomerSettings customerSettings = null;
    try {
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            if (Integer.parseInt(method.getResponseHeader("Content-Length").getValue()) > 0) {
                customerSettings = deserializeXMLToCustomerSettings(method.getResponseBodyAsStream());
            } else {
                return null;
            }
        }
    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
    return customerSettings;
}

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

public List<RubricCriteriaItem> getRubricCriteria(int learningObjectId, int instanceId) throws Exception {
    String uri = String.format(
            _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/RubricCriteria",
            learningObjectId, instanceId);
    HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET);
    List<RubricCriteriaItem> criteriaCreator = new ArrayList<RubricCriteriaItem>();
    try {//from  w  ww  . ja  v a 2s. c  o  m
        int statusCode = _httpClient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new HTTPException(statusCode);
        } else {
            if (Integer.parseInt(method.getResponseHeader("Content-Length").getValue()) > 0) {
                criteriaCreator = deserializeXMLToCriteria(method.getResponseBodyAsStream());
            } else {
                return null;
            }
        }
    } catch (Exception ex) {
        ExceptionHandler.handle(ex);
    } finally {
        method.releaseConnection();
    }
    return criteriaCreator;
}