Search in sources :

Example 6 with SeriesException

use of org.opencastproject.series.api.SeriesException in project opencast by opencast.

the class SeriesServiceRemoteImpl method getIdTitleMapOfAllSeries.

@Override
public Map<String, String> getIdTitleMapOfAllSeries() throws SeriesException, UnauthorizedException {
    HttpGet get = new HttpGet("/allSeriesIdTitle.json");
    HttpResponse response = getResponse(get, SC_OK, SC_UNAUTHORIZED, SC_INTERNAL_SERVER_ERROR);
    try {
        if (response != null) {
            int statusCode = response.getStatusLine().getStatusCode();
            if (SC_UNAUTHORIZED == statusCode) {
                throw new UnauthorizedException("Not authorized to get series");
            } else if (SC_OK == statusCode) {
                String seriesJSON = EntityUtils.toString(response.getEntity(), "UTF-8");
                Object resultContainer = new JSONParser().parse(seriesJSON);
                if (resultContainer instanceof JSONObject) {
                    Map<String, String> result = new HashMap<>();
                    JSONObject resultContainerJsonObj = (JSONObject) resultContainer;
                    JSONArray seriesJsonArr = resultContainerJsonObj.optJSONArray("series");
                    if (seriesJsonArr != null) {
                        for (int idx = 0; idx < seriesJsonArr.length(); idx++) {
                            JSONObject seriesJsonObj = seriesJsonArr.getJSONObject(idx);
                            String seriesId = seriesJsonObj.optString("identifier");
                            String seriesTitle = seriesJsonObj.optString("title");
                            if (StringUtils.isNotBlank(seriesId) && StringUtils.isNotEmpty(seriesTitle))
                                result.put(seriesId, seriesTitle);
                        }
                    }
                    return result;
                }
            }
        }
    } catch (UnauthorizedException e) {
        throw e;
    } catch (Exception e) {
        throw new SeriesException("Unable to get series from remote series index: " + e);
    } finally {
        closeConnection(response);
    }
    throw new SeriesException("Unable to get series from remote series index");
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.codehaus.jettison.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) SeriesException(org.opencastproject.series.api.SeriesException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) JSONObject(org.codehaus.jettison.json.JSONObject) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONParser(org.json.simple.parser.JSONParser) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 7 with SeriesException

use of org.opencastproject.series.api.SeriesException in project opencast by opencast.

the class SeriesServiceRemoteImpl method updateOptOutStatus.

@Override
public void updateOptOutStatus(String seriesId, boolean optOut) throws NotFoundException, SeriesException {
    HttpPost post = new HttpPost("/optOutSeries/" + optOut);
    HttpResponse response = null;
    try {
        JSONArray seriesIds = new JSONArray();
        seriesIds.put(seriesId);
        post.setEntity(new StringEntity(seriesIds.toString()));
        response = getResponse(post, SC_OK);
        BulkOperationResult bulkOperationResult = new BulkOperationResult();
        bulkOperationResult.fromJson(response.getEntity().getContent());
        if (bulkOperationResult.getNotFound().size() > 0) {
            throw new NotFoundException("Unable to find series with id " + seriesId);
        } else if (bulkOperationResult.getServerError().size() > 0) {
            throw new SeriesException("Unable to update series " + seriesId + " opt out status using the remote series services.");
        }
    } catch (Exception e) {
        throw new SeriesException("Unable to assemble a remote series request for updating series " + seriesId + " with optOut status of " + optOut + " because:" + ExceptionUtils.getStackTrace(e));
    } finally {
        if (response != null) {
            closeConnection(response);
        }
    }
    throw new SeriesException("Unable to update series " + seriesId + " using the remote series services");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) JSONArray(org.codehaus.jettison.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) BulkOperationResult(org.opencastproject.rest.BulkOperationResult) SeriesException(org.opencastproject.series.api.SeriesException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 8 with SeriesException

use of org.opencastproject.series.api.SeriesException in project opencast by opencast.

the class SeriesServiceRemoteImpl method getSeriesElements.

@Override
public Opt<Map<String, byte[]>> getSeriesElements(String seriesID) throws SeriesException {
    HttpGet get = new HttpGet("/" + seriesID + "/elements.json");
    HttpResponse response = getResponse(get, SC_OK, SC_NOT_FOUND, SC_INTERNAL_SERVER_ERROR);
    JSONParser parser = new JSONParser();
    try {
        if (response == null) {
            throw new SeriesException(format("Error while retrieving elements from series '%s'", seriesID));
        } else {
            final int statusCode = response.getStatusLine().getStatusCode();
            switch(statusCode) {
                case SC_OK:
                    JSONArray elementArray = (JSONArray) parser.parse(IOUtils.toString(response.getEntity().getContent()));
                    Map<String, byte[]> elements = new HashMap<>();
                    for (int i = 0; i < elementArray.length(); i++) {
                        final String type = elementArray.getString(i);
                        Opt<byte[]> optData = getSeriesElementData(seriesID, type);
                        if (optData.isSome()) {
                            elements.put(type, optData.get());
                        } else {
                            throw new SeriesException(format("Tried to load non-existing element of type '%s'", type));
                        }
                    }
                    return Opt.some(elements);
                case SC_NOT_FOUND:
                    return Opt.none();
                case SC_INTERNAL_SERVER_ERROR:
                    throw new SeriesException(format("Error while retrieving elements from series '%s'", seriesID));
                default:
                    throw new SeriesException(format("Unexpected status code", statusCode));
            }
        }
    } catch (Exception e) {
        logger.warn("Error while retrieving elements from remote service: %s", ExceptionUtils.getStackTrace(e));
        throw new SeriesException(e);
    } finally {
        closeConnection(response);
    }
}
Also used : HashMap(java.util.HashMap) HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.codehaus.jettison.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) JSONParser(org.json.simple.parser.JSONParser) SeriesException(org.opencastproject.series.api.SeriesException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 9 with SeriesException

use of org.opencastproject.series.api.SeriesException in project opencast by opencast.

the class SeriesServiceRemoteImpl method getSeriesProperty.

@Override
public String getSeriesProperty(String seriesID, String propertyName) throws SeriesException, NotFoundException, UnauthorizedException {
    HttpGet get = new HttpGet(seriesID + "/property/" + propertyName + ".json");
    HttpResponse response = getResponse(get, SC_OK, SC_NOT_FOUND, SC_UNAUTHORIZED);
    try {
        if (response != null) {
            if (SC_NOT_FOUND == response.getStatusLine().getStatusCode()) {
                throw new NotFoundException("Series " + seriesID + " not found in remote series index!");
            } else if (SC_UNAUTHORIZED == response.getStatusLine().getStatusCode()) {
                throw new UnauthorizedException("Not authorized to get series " + seriesID);
            } else {
                logger.debug("Successfully received series {} property {} from the remote series index", seriesID, propertyName);
                StringWriter writer = new StringWriter();
                IOUtils.copy(response.getEntity().getContent(), writer, "UTF-8");
                return writer.toString();
            }
        }
    } catch (UnauthorizedException e) {
        throw e;
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        throw new SeriesException("Unable to parse series from remote series index: " + e);
    } finally {
        closeConnection(response);
    }
    throw new SeriesException("Unable to get series from remote series index");
}
Also used : StringWriter(java.io.StringWriter) HttpGet(org.apache.http.client.methods.HttpGet) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) HttpResponse(org.apache.http.HttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) SeriesException(org.opencastproject.series.api.SeriesException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 10 with SeriesException

use of org.opencastproject.series.api.SeriesException in project opencast by opencast.

the class SeriesServiceRemoteImpl method getSeriesElementData.

@Override
public Opt<byte[]> getSeriesElementData(String seriesID, String type) throws SeriesException {
    HttpGet get = new HttpGet("/" + seriesID + "/elements/" + type);
    HttpResponse response = getResponse(get, SC_OK, SC_NOT_FOUND, SC_INTERNAL_SERVER_ERROR);
    try {
        if (response == null) {
            throw new SeriesException(format("Error while retrieving element of type '%s' from series '%s'", type, seriesID));
        } else {
            final int statusCode = response.getStatusLine().getStatusCode();
            switch(statusCode) {
                case SC_OK:
                    return Opt.some(IOUtils.toByteArray(response.getEntity().getContent()));
                case SC_NOT_FOUND:
                    return Opt.none();
                case SC_INTERNAL_SERVER_ERROR:
                    throw new SeriesException(format("Error while retrieving element of type '%s' from series '%s'", type, seriesID));
                default:
                    throw new SeriesException(format("Unexpected status code", statusCode));
            }
        }
    } catch (Exception e) {
        logger.warn("Error while retrieving element from remote service: %s", ExceptionUtils.getStackTrace(e));
        throw new SeriesException(e);
    } finally {
        closeConnection(response);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) SeriesException(org.opencastproject.series.api.SeriesException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException)

Aggregations

SeriesException (org.opencastproject.series.api.SeriesException)40 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)24 NotFoundException (org.opencastproject.util.NotFoundException)24 HttpResponse (org.apache.http.HttpResponse)19 WebApplicationException (javax.ws.rs.WebApplicationException)16 ParseException (java.text.ParseException)14 HttpGet (org.apache.http.client.methods.HttpGet)10 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)6 AccessControlList (org.opencastproject.security.api.AccessControlList)6 HashMap (java.util.HashMap)5 Path (javax.ws.rs.Path)5 RestQuery (org.opencastproject.util.doc.rest.RestQuery)5 InputStream (java.io.InputStream)4 Map (java.util.Map)4 HttpPost (org.apache.http.client.methods.HttpPost)4 JSONArray (org.codehaus.jettison.json.JSONArray)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 GET (javax.ws.rs.GET)3