use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionRelationsResource method getQuestions.
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "A list of relations from the network", response = QuestionRelationList.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No space id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response getQuestions(@PathParam("spaceId") String spaceId, @QueryParam("order") String order, @QueryParam("limit") Integer limit, @QueryParam("startAt") Integer startAt) throws ServiceInvocationException {
QuestionRelationList questionRelationList = getQuestionRelations(spaceId, order, limit, startAt);
VotedQuestionRelationList votedQuestionRelationList = new VotedQuestionRelationList();
for (QuestionRelation questionRelation : questionRelationList) {
VotedQuestionRelation votedQuestionRelation = new VotedQuestionRelation(questionRelation);
String objectId = RelationVotesResource.buildObjectId(spaceId, questionRelation.getRelationId());
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
if (rmiResult instanceof VoteList) {
votedQuestionRelation.setVotes((VoteList) rmiResult);
}
votedQuestionRelationList.add(votedQuestionRelation);
}
ResponseBuilder responseBuilder = Response.ok(votedQuestionRelationList);
String queryOrder = order != null ? "order=" + order : "";
String queryLimit = limit != null ? "limit=" + Integer.toString(limit) : "";
String queryStartAt = "";
if (startAt != null && limit != null) {
if (order.equalsIgnoreCase("desc")) {
queryStartAt = "startat=" + Integer.toString(startAt - limit);
} else {
queryStartAt = "startat=" + Integer.toString(startAt + limit);
}
}
String nextLinkStr = "";
for (String param : new String[] { queryOrder, queryLimit, queryStartAt }) {
if (param != null && !param.isEmpty()) {
if (nextLinkStr.isEmpty()) {
nextLinkStr += "?";
} else {
nextLinkStr += "&";
}
nextLinkStr += param;
}
}
if (!nextLinkStr.isEmpty()) {
responseBuilder.header(HttpHeaders.LINK, "<" + nextLinkStr + ">; rel=\"next\"");
}
return responseBuilder.build();
}
use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.
the class QuestionsResource method getQuestionsWeb.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "A list of questions from the network", response = QuestionList.class), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No space id given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access Denied", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response getQuestionsWeb(@PathParam("spaceId") String spaceId, @QueryParam("order") String order, @QueryParam("limit") Integer limit, @QueryParam("startat") Integer startAt) throws ServiceInvocationException {
QuestionList questionList = getQuestions(spaceId, order, limit, startAt);
VotedQuestionList votedQuestionList = new VotedQuestionList();
for (Question question : questionList) {
VotedQuestion votedQuestion = new VotedQuestion(question);
String objectId = QuestionVotesResource.buildObjectId(spaceId, question.getQuestionId());
Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleVoteService.class.getCanonicalName(), NoracleService.API_VERSION), "getAllVotes", objectId);
if (rmiResult instanceof VoteList) {
votedQuestion.setVotes((VoteList) rmiResult);
}
votedQuestionList.add(votedQuestion);
}
ResponseBuilder responseBuilder = Response.ok(votedQuestionList);
String queryOrder = order != null ? "order=" + order : "";
String queryLimit = limit != null ? "limit=" + Integer.toString(limit) : "";
String queryStartAt = "";
if (startAt != null && limit != null) {
if (order.equalsIgnoreCase("desc")) {
queryStartAt = "startat=" + Integer.toString(startAt - limit);
} else {
queryStartAt = "startat=" + Integer.toString(startAt + limit);
}
}
String nextLinkStr = "";
for (String param : new String[] { queryOrder, queryLimit, queryStartAt }) {
if (param != null && !param.isEmpty()) {
if (nextLinkStr.isEmpty()) {
nextLinkStr += "?";
} else {
nextLinkStr += "&";
}
nextLinkStr += param;
}
}
if (!nextLinkStr.isEmpty()) {
responseBuilder.header(HttpHeaders.LINK, "<" + nextLinkStr + ">; rel=\"next\"");
}
return responseBuilder.build();
}
use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionRelationService method getQuestionRelation.
@Override
public QuestionRelation getQuestionRelation(String relationId) throws ServiceInvocationException {
if (relationId == null || relationId.isEmpty()) {
throw new InvocationBadArgumentException("No relation id given");
}
Envelope env;
try {
env = Context.get().requestEnvelope(getQuestionRelationEnvelopeIdentifier(relationId));
} catch (EnvelopeAccessDeniedException e) {
throw new ServiceAccessDeniedException("Envelope Access Denied");
} catch (EnvelopeOperationFailedException e) {
throw new InternalServiceException("Could not fetch relation envelope", e);
} catch (EnvelopeNotFoundException e) {
throw new ResourceNotFoundException("Relation Not Found");
}
QuestionRelation relation = (QuestionRelation) env.getContent();
return relation;
}
use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleVoteService method getAgentVote.
@Override
public Vote getAgentVote(String objectId, String agentId) throws ServiceInvocationException {
String persEnvId = getAgentVoteEnvelopeIdentifier(agentId, objectId);
try {
Envelope persEnv = Context.get().requestEnvelope(persEnvId);
VoteEntry voteEntry = (VoteEntry) persEnv.getContent();
return voteEntry.getVote();
} catch (EnvelopeAccessDeniedException e) {
throw new InternalServiceException("Someone hijacked your vote envelope", e);
} catch (EnvelopeNotFoundException e) {
return new Vote(0, agentId);
} catch (EnvelopeOperationFailedException e) {
throw new InternalServiceException("Retrieving vote failed", e);
}
}
Aggregations