Search in sources :

Example 11 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleSpaceService method getSubscribers.

@Override
public SpaceSubscribersList getSubscribers(String spaceId) throws ServiceInvocationException {
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    }
    try {
        UserAgentImpl inviteAgent = this.getInviteAgent(spaceId);
        GroupAgent memberAgent = this.getMemberAgent(spaceId);
        memberAgent.unlock(Context.get().getMainAgent());
        String[] memberIds = memberAgent.getMemberList();
        SpaceSubscribersList subscribers = new SpaceSubscribersList();
        for (int i = 0; i < memberIds.length; i++) {
            if (!memberIds[i].equals(inviteAgent.getIdentifier())) {
                UserAgentImpl agent = (UserAgentImpl) Context.get().fetchAgent(memberIds[i]);
                NoracleAgentProfile profile = new NoracleAgentProfile();
                profile.setName(agent.getLoginName());
                subscribers.add(profile);
            }
        }
        return subscribers;
    } catch (Exception e) {
        throw new ServiceInvocationException("Could not fetch list", e);
    }
}
Also used : NoracleAgentProfile(i5.las2peer.services.noracleService.model.NoracleAgentProfile) UserAgentImpl(i5.las2peer.security.UserAgentImpl) SpaceSubscribersList(i5.las2peer.services.noracleService.model.SpaceSubscribersList) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) CryptoException(i5.las2peer.tools.CryptoException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) SerializationException(i5.las2peer.serialization.SerializationException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException)

Example 12 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionRelationsResource method createQuestionRelation.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "Relation successfully created"), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "You have to be logged in to create a relation", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response createQuestionRelation(@PathParam("spaceId") String spaceId, @ApiParam(required = true) CreateRelationPojo createRelationPojo) throws ServiceInvocationException {
    QuestionRelation rel = createQuestionRelation(spaceId, createRelationPojo.getName(), createRelationPojo.getFirstQuestionId(), createRelationPojo.getSecondQuestionId(), createRelationPojo.isDirected());
    try {
        Gson gson = new Gson();
        String createRelationPojoJson = gson.toJson(createRelationPojo);
        JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
        JSONObject obj = (JSONObject) p.parse(createRelationPojoJson);
        obj.put("spaceId", spaceId);
        obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_3, obj.toJSONString());
        // Try to log training data
        try {
            String from = getQuestionText(createRelationPojo.getFirstQuestionId());
            String to = getQuestionText(createRelationPojo.getSecondQuestionId());
            JSONObject trainingData = new JSONObject();
            trainingData.put("unit", spaceId);
            trainingData.put("from", from);
            trainingData.put("to", to);
            if (createRelationPojo.isDirected())
                Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_40, trainingData.toString());
            else
                Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_41, trainingData.toString());
        } catch (ServiceInvocationException | NullPointerException e) {
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_ERROR_40, e.getMessage());
        }
        Question q1 = null;
        Question q2 = null;
        Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", createRelationPojo.getFirstQuestionId());
        if (rmiResult instanceof Question) {
            q1 = (Question) rmiResult;
        }
        Serializable rmiResult2 = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", createRelationPojo.getSecondQuestionId());
        if (rmiResult instanceof Question) {
            q2 = (Question) rmiResult2;
        }
        if (q1 != null && q2 != null) {
            try {
                Instant timestamp = Instant.parse(q1.getTimestampCreated());
                Instant timestamp2 = Instant.parse(q2.getTimestampCreated());
                if (timestamp.isBefore(timestamp2) && q2.getDepth() < 1) {
                    changeQuestionDepth(q2.getQuestionId(), q1.getDepth() + 1);
                    JSONObject obj2 = new JSONObject();
                    obj2.put("spaceId", spaceId);
                    obj2.put("qid", q2.getQuestionId());
                    obj2.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
                    obj2.put("depth", q1.getDepth() + 1);
                    Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_10, obj2.toJSONString());
                } else {
                    if (q1.getDepth() < 1) {
                        changeQuestionDepth(q1.getQuestionId(), q2.getDepth() + 1);
                        JSONObject obj2 = new JSONObject();
                        obj2.put("spaceId", spaceId);
                        obj2.put("qid", q1.getQuestionId());
                        obj2.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
                        obj2.put("depth", q2.getDepth() + 1);
                        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_10, obj2.toJSONString());
                    }
                }
            } catch (Exception e) {
            }
        }
        return Response.created(new URI(null, null, SpacesResource.RESOURCE_NAME + "/" + spaceId + "/" + RESOURCE_NAME + "/" + rel.getRelationId(), null)).build();
    } catch (URISyntaxException | ParseException e) {
        throw new InternalServerErrorException(e);
    }
}
Also used : Serializable(java.io.Serializable) Instant(java.time.Instant) Gson(com.google.gson.Gson) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ParseException(net.minidev.json.parser.ParseException) InternalServiceException(i5.las2peer.api.execution.InternalServiceException) JSONObject(net.minidev.json.JSONObject) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ApiResponses(io.swagger.annotations.ApiResponses)

Example 13 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionsResource method createRelatedQuestion.

@POST
@Path("/{questionId}/relation")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "Question successfully created"), @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "No question text given", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "You have to be logged in to create a question", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response createRelatedQuestion(@PathParam("spaceId") String questionSpaceId, @PathParam("questionId") String fquestionId, @ApiParam(required = true) CreateQuestionPojo createQuestionPojo) throws ServiceInvocationException {
    Question question = createQuestion(questionSpaceId, createQuestionPojo.getText());
    try {
        // CREATE QUESTION
        Gson gson = new Gson();
        String createQuestionPojoJson = gson.toJson(createQuestionPojo);
        JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
        JSONObject obj = new JSONObject();
        JSONObject attributes = new JSONObject();
        obj.put("functionName", "createQuestion");
        obj.put("serviceAlias", "distributed-noracle");
        obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
        obj.put("qid", question.getQuestionId());
        attributes.put("spaceId", questionSpaceId);
        attributes.put("body", p.parse(createQuestionPojoJson));
        attributes.put("result", question.getQuestionId());
        obj.put("attributes", attributes);
        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_5, obj.toJSONString());
        // CREATE RELATION
        CreateRelationPojo createRelationPojo = new CreateRelationPojo();
        createRelationPojo.setDirected(true);
        createRelationPojo.setFirstQuestionId(fquestionId);
        createRelationPojo.setSecondQuestionId(question.getQuestionId());
        createRelationPojo.setName("FollowUp");
        // createRelationPojo.isDirected());
        try {
            String createRelationPojoJson = gson.toJson(createRelationPojo);
            obj = (JSONObject) p.parse(createRelationPojoJson);
            obj.put("spaceId", questionSpaceId);
            obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_3, obj.toJSONString());
            // Try to log training data
            try {
                String from = getQuestionText(createRelationPojo.getFirstQuestionId());
                String to = getQuestionText(createRelationPojo.getSecondQuestionId());
                JSONObject trainingData = new JSONObject();
                trainingData.put("unit", questionSpaceId);
                trainingData.put("from", from);
                trainingData.put("to", to);
                if (createRelationPojo.isDirected())
                    Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_40, trainingData.toString());
                else
                    Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_41, trainingData.toString());
            } catch (ServiceInvocationException | NullPointerException e) {
                Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_ERROR_40, e.getMessage());
            }
            Question q1 = null;
            Question q2 = null;
            Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", createRelationPojo.getFirstQuestionId());
            if (rmiResult instanceof Question) {
                q1 = (Question) rmiResult;
            }
            Serializable rmiResult2 = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", createRelationPojo.getSecondQuestionId());
            if (rmiResult instanceof Question) {
                q2 = (Question) rmiResult2;
            }
            if (q1 != null && q2 != null) {
                try {
                    Instant timestamp = Instant.parse(q1.getTimestampCreated());
                    Instant timestamp2 = Instant.parse(q2.getTimestampCreated());
                    if (timestamp.isBefore(timestamp2) && q2.getDepth() < 1) {
                        changeQuestionDepth(q2.getQuestionId(), q1.getDepth() + 1);
                        JSONObject obj2 = new JSONObject();
                        obj2.put("spaceId", questionSpaceId);
                        obj2.put("qid", q2.getQuestionId());
                        obj2.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
                        obj2.put("depth", q1.getDepth() + 1);
                        Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_10, obj2.toJSONString());
                    } else {
                        if (q1.getDepth() < 1) {
                            changeQuestionDepth(q1.getQuestionId(), q2.getDepth() + 1);
                            JSONObject obj2 = new JSONObject();
                            obj2.put("spaceId", questionSpaceId);
                            obj2.put("qid", q1.getQuestionId());
                            obj2.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
                            obj2.put("depth", q2.getDepth() + 1);
                            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_10, obj2.toJSONString());
                        }
                    }
                } catch (Exception e) {
                }
            }
        } catch (ParseException e) {
            throw new InternalServerErrorException(e);
        }
        return Response.created(new URI(null, null, SpacesResource.RESOURCE_NAME + "/" + questionSpaceId + "/" + RESOURCE_NAME + "/" + question.getQuestionId(), null)).build();
    } catch (URISyntaxException | ParseException e) {
        throw new InternalServerErrorException(e);
    }
}
Also used : Serializable(java.io.Serializable) Instant(java.time.Instant) Gson(com.google.gson.Gson) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ParseException(net.minidev.json.parser.ParseException) InternalServiceException(i5.las2peer.api.execution.InternalServiceException) CreateRelationPojo(i5.las2peer.services.noracleService.pojo.CreateRelationPojo) JSONObject(net.minidev.json.JSONObject) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) ApiResponses(io.swagger.annotations.ApiResponses)

Example 14 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionsResource method getQuestionText.

private String getQuestionText(String questionId) throws ServiceInvocationException {
    String qText = "";
    Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleQuestionService.class.getCanonicalName(), NoracleService.API_VERSION), "getQuestion", questionId);
    if (rmiResult instanceof Question)
        qText = ((Question) rmiResult).getText();
    return qText;
}
Also used : Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion)

Example 15 with ServiceInvocationException

use of i5.las2peer.api.execution.ServiceInvocationException in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleVoteService method setVote.

@Override
public Vote setVote(String agentId, String objectId, int vote) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (objectId == null || objectId.isEmpty()) {
        throw new InvocationBadArgumentException("No object id given");
    } else if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceAccessDeniedException("You have to be logged in to vote");
    }
    String persEnvId = getAgentVoteEnvelopeIdentifier(agentId, objectId);
    try {
        try {
            Envelope persEnv = Context.get().requestEnvelope(persEnvId);
            VoteEntry voteEntry = (VoteEntry) persEnv.getContent();
            int pubIndex = voteEntry.getPubIndex();
            String pubEnvId = getPublicVoteEnvelopeIdentifier(objectId, pubIndex);
            Vote pubVote = updateOrCreatePubVoteEnv(pubEnvId, vote, mainAgent.getIdentifier());
            voteEntry = (VoteEntry) persEnv.getContent();
            voteEntry.setVote(pubVote);
            persEnv.setContent(voteEntry);
            Context.get().storeEnvelope(persEnv);
            return pubVote;
        } catch (EnvelopeNotFoundException e) {
            Envelope persEnv = Context.get().createEnvelope(persEnvId);
            String pubEnvId = null;
            int pubIndex = -1;
            for (int num = 1; num < MAX_VOTES_PER_OBJECT; num++) {
                try {
                    pubEnvId = getPublicVoteEnvelopeIdentifier(objectId, num);
                    Context.get().requestEnvelope(pubEnvId);
                } catch (EnvelopeNotFoundException e2) {
                    // found non taken vote index
                    pubIndex = num;
                    break;
                } catch (Exception e2) {
                // XXX logging
                }
            }
            if (pubEnvId == null || pubEnvId.isEmpty()) {
                throw new InternalServiceException("Public envelope id is null");
            } else if (pubIndex == -1) {
                throw new InternalServiceException("Too many votes for this object");
            }
            Vote pubVote = updateOrCreatePubVoteEnv(pubEnvId, vote, mainAgent.getIdentifier());
            VoteEntry voteEntry = new VoteEntry(objectId, pubIndex, pubVote);
            persEnv.setContent(voteEntry);
            Context.get().storeEnvelope(persEnv);
            return pubVote;
        }
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create envelope for vote", e);
    }
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) Vote(i5.las2peer.services.noracleService.model.Vote) VoteEntry(i5.las2peer.services.noracleService.model.VoteEntry) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) InvocationBadArgumentException(i5.las2peer.api.execution.InvocationBadArgumentException) AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) ServiceAccessDeniedException(i5.las2peer.api.execution.ServiceAccessDeniedException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) InvocationBadArgumentException(i5.las2peer.api.execution.InvocationBadArgumentException) ServiceAccessDeniedException(i5.las2peer.api.execution.ServiceAccessDeniedException) ServiceInvocationException(i5.las2peer.api.execution.ServiceInvocationException) InternalServiceException(i5.las2peer.api.execution.InternalServiceException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) InternalServiceException(i5.las2peer.api.execution.InternalServiceException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException)

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