Search in sources :

Example 1 with SpaceSubscriptionList

use of i5.las2peer.services.noracleService.model.SpaceSubscriptionList 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 SpaceSubscriptionList

use of i5.las2peer.services.noracleService.model.SpaceSubscriptionList in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleAgentService method updateSpaceSubscription.

@Override
public SpaceSubscription updateSpaceSubscription(String agentId, String spaceId, String[] selectedQuestions) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    String envIdentifier = buildSubscriptionId(agentId);
    try {
        Envelope env = Context.get().requestEnvelope(envIdentifier);
        SpaceSubscriptionList spaceSubscriptionList = (SpaceSubscriptionList) env.getContent();
        for (SpaceSubscription spaceSubscription : spaceSubscriptionList) {
            if (spaceSubscription.getSpaceId().equals(spaceId)) {
                spaceSubscription.setSelectedQuestionIds(selectedQuestions);
                env.setContent(spaceSubscriptionList);
                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 spaceSubscription;
            }
        }
        throw new ResourceNotFoundException("Space subscription not found");
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope Access Denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not fetch space subscription envelope", e);
    } catch (EnvelopeNotFoundException e) {
        throw new ResourceNotFoundException("No space subscriptions found");
    }
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) SpaceSubscriptionList(i5.las2peer.services.noracleService.model.SpaceSubscriptionList) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) SpaceSubscription(i5.las2peer.services.noracleService.model.SpaceSubscription)

Example 3 with SpaceSubscriptionList

use of i5.las2peer.services.noracleService.model.SpaceSubscriptionList 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)3 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)3 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)3 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)3 Agent (i5.las2peer.api.security.Agent)3 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)3 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)3 SpaceSubscriptionList (i5.las2peer.services.noracleService.model.SpaceSubscriptionList)3 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)1