Search in sources :

Example 1 with AnonymousAgent

use of i5.las2peer.api.security.AnonymousAgent in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleAgentService method unsubscribeFromSpace.

@Override
public void unsubscribeFromSpace(String spaceId) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    } else if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceAccessDeniedException("You have to be logged in to unsubscribe to a space");
    }
    String envIdentifier = buildSubscriptionId(mainAgent.getIdentifier());
    Envelope env;
    SpaceSubscriptionList subscriptionList;
    try {
        try {
            env = Context.get().requestEnvelope(envIdentifier);
            subscriptionList = (SpaceSubscriptionList) env.getContent();
        } catch (EnvelopeNotFoundException e) {
            return;
        }
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not read envelope for space unsubscription", e);
    }
    Iterator<SpaceSubscription> itSubscription = subscriptionList.iterator();
    while (itSubscription.hasNext()) {
        SpaceSubscription subscription = itSubscription.next();
        if (subscription.getSpaceId().equals(spaceId)) {
            itSubscription.remove();
        }
    }
    env.setContent(subscriptionList);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store space subscription envelope", e);
    }
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) SpaceSubscriptionList(i5.las2peer.services.noracleService.model.SpaceSubscriptionList) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) SpaceSubscription(i5.las2peer.services.noracleService.model.SpaceSubscription)

Example 2 with AnonymousAgent

use of i5.las2peer.api.security.AnonymousAgent in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleQuestionService method createQuestion.

@Override
public Question createQuestion(String questionSpaceId, String text) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (questionSpaceId == null || questionSpaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No question space id given");
    } else if (text == null || text.isEmpty()) {
        throw new InvocationBadArgumentException("No question text given");
    } else if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceNotAuthorizedException("You have to be logged in to create a question");
    }
    Space targetSpace;
    Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleSpaceService.class.getCanonicalName(), NoracleService.API_VERSION), "getSpace", questionSpaceId);
    if (rmiResult instanceof Space) {
        targetSpace = (Space) rmiResult;
    } else {
        throw new InternalServiceException("Unexpected result (" + rmiResult.getClass().getCanonicalName() + ") of RMI call");
    }
    String targetReaderGroupId = targetSpace.getSpaceReaderGroupId();
    GroupAgent targetReaderGroup;
    try {
        targetReaderGroup = (GroupAgent) Context.get().requestAgent(targetReaderGroupId, mainAgent);
    } catch (AgentNotFoundException | AgentOperationFailedException e) {
        throw new InternalServiceException("Could not fetch reader group agent for space", e);
    } catch (ClassCastException e) {
        throw new InternalServiceException("Agent for space reader group is not a GroupAgent", e);
    } catch (AgentAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Agent not in space reader group", e);
    }
    String questionId = buildQuestionId();
    Envelope env;
    try {
        env = Context.get().createEnvelope(getQuestionEnvelopeIdentifier(questionId), mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create envelope for question", e);
    }
    env.addReader(targetReaderGroup);
    Question question = new Question(questionId, text, questionSpaceId, mainAgent.getIdentifier(), Instant.now().toString());
    env.setContent(question);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store question envelope", e);
    }
    if (questionSpaceId != null && !questionSpaceId.isEmpty()) {
        linkQuestionToSpace(questionSpaceId, questionId);
    }
    return question;
}
Also used : Space(i5.las2peer.services.noracleService.model.Space) Serializable(java.io.Serializable) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) Question(i5.las2peer.services.noracleService.model.Question)

Example 3 with AnonymousAgent

use of i5.las2peer.api.security.AnonymousAgent 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)

Example 4 with AnonymousAgent

use of i5.las2peer.api.security.AnonymousAgent in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleQuestionRelationService method createQuestionRelation.

@Override
public QuestionRelation createQuestionRelation(String spaceId, String name, String questionId1, String questionId2, Boolean directed) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceAccessDeniedException("You have to be logged in to create a relation");
    }
    String relationId = buildQuestionRelationId();
    Envelope env;
    try {
        env = Context.get().createEnvelope(getQuestionRelationEnvelopeIdentifier(relationId));
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create envelope for relation", e);
    }
    env.setPublic();
    String strNow = Instant.now().toString();
    QuestionRelation relation = new QuestionRelation(relationId, spaceId, mainAgent.getIdentifier(), name, questionId1, questionId2, directed, strNow);
    env.setContent(relation);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store relation envelope", e);
    }
    if (spaceId != null && !spaceId.isEmpty()) {
        linkQuestionRelationToSpace(spaceId, relationId);
    }
    return relation;
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) QuestionRelation(i5.las2peer.services.noracleService.model.QuestionRelation) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope)

Example 5 with AnonymousAgent

use of i5.las2peer.api.security.AnonymousAgent in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleAgentService method subscribeToSpace.

@Override
public SpaceSubscription subscribeToSpace(String spaceId, String spaceSecret) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    } else if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceAccessDeniedException("You have to be logged in to subscribe to a space");
    }
    Context.get().invoke(new ServiceNameVersion(NoracleSpaceService.class.getCanonicalName(), NoracleService.API_VERSION), "joinSpace", spaceId, spaceSecret);
    SpaceSubscription subscription = new SpaceSubscription(spaceId, spaceSecret);
    String envIdentifier = buildSubscriptionId(mainAgent.getIdentifier());
    Envelope env;
    SpaceSubscriptionList subscriptionList;
    try {
        try {
            env = Context.get().requestEnvelope(envIdentifier);
            subscriptionList = (SpaceSubscriptionList) env.getContent();
        } catch (EnvelopeNotFoundException e) {
            env = Context.get().createEnvelope(envIdentifier);
            subscriptionList = new SpaceSubscriptionList();
        }
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create envelope for space subscription", e);
    }
    subscriptionList.add(subscription);
    env.setContent(subscriptionList);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store space subscription envelope", e);
    }
    return subscription;
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) SpaceSubscriptionList(i5.las2peer.services.noracleService.model.SpaceSubscriptionList) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) SpaceSubscription(i5.las2peer.services.noracleService.model.SpaceSubscription)

Aggregations

Envelope (i5.las2peer.api.persistency.Envelope)6 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)6 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)6 Agent (i5.las2peer.api.security.Agent)4 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)4 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)3 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)2 Space (i5.las2peer.services.noracleService.model.Space)2 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)2 SpaceSubscriptionList (i5.las2peer.services.noracleService.model.SpaceSubscriptionList)2 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)1 InvocationBadArgumentException (i5.las2peer.api.execution.InvocationBadArgumentException)1 ServiceAccessDeniedException (i5.las2peer.api.execution.ServiceAccessDeniedException)1 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)1 GroupAgentImpl (i5.las2peer.security.GroupAgentImpl)1 SerializationException (i5.las2peer.serialization.SerializationException)1 Question (i5.las2peer.services.noracleService.model.Question)1 QuestionRelation (i5.las2peer.services.noracleService.model.QuestionRelation)1 SpaceInviteAgent (i5.las2peer.services.noracleService.model.SpaceInviteAgent)1 Vote (i5.las2peer.services.noracleService.model.Vote)1