Search in sources :

Example 31 with ServiceInvocationException

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();
}
Also used : Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ApiResponses(io.swagger.annotations.ApiResponses)

Example 32 with ServiceInvocationException

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();
}
Also used : Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ApiResponses(io.swagger.annotations.ApiResponses)

Example 33 with ServiceInvocationException

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;
}
Also used : EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) QuestionRelation(i5.las2peer.services.noracleService.model.QuestionRelation) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope)

Example 34 with ServiceInvocationException

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);
    }
}
Also used : Vote(i5.las2peer.services.noracleService.model.Vote) VoteEntry(i5.las2peer.services.noracleService.model.VoteEntry) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) InternalServiceException(i5.las2peer.api.execution.InternalServiceException)

Aggregations

EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)19 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)19 Envelope (i5.las2peer.api.persistency.Envelope)17 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)16 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)15 Serializable (java.io.Serializable)13 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)9 ApiResponses (io.swagger.annotations.ApiResponses)7 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)6 Agent (i5.las2peer.api.security.Agent)6 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)6 Gson (com.google.gson.Gson)5 Question (i5.las2peer.services.noracleService.model.Question)5 Space (i5.las2peer.services.noracleService.model.Space)5 Vote (i5.las2peer.services.noracleService.model.Vote)5 JSONObject (net.minidev.json.JSONObject)5 JSONParser (net.minidev.json.parser.JSONParser)5 ParseException (net.minidev.json.parser.ParseException)5 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)4 SerializationException (i5.las2peer.serialization.SerializationException)3