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");
}
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");
}
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);
}
}
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");
}
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);
}
}
Aggregations