Search in sources :

Example 6 with Agent

use of i5.las2peer.api.security.Agent 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 7 with Agent

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

the class NoracleServiceTest method beforeTest.

/**
 * Called before a test starts.
 *
 * Sets up the node, initializes connector and adds user agent that can be used throughout the test.
 *
 * @throws Exception
 */
@Before
public void beforeTest() {
    try {
        nodes = TestSuite.launchNetwork(networkSize);
        connector = new WebConnector(null);
        connector.start(nodes.get(0));
        // don't follow redirects, some tests want to test for correct redirect
        // responses
        webClient = ClientBuilder.newBuilder().register(MultiPartFeature.class).property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE).build();
        startService(nodes.get(0), "i5.las2peer.services.noracleService.NoracleService", NoracleService.API_VERSION);
        startService(nodes.get(0), "i5.las2peer.services.noracleService.NoracleAgentService", NoracleService.API_VERSION);
        startService(nodes.get(0), "i5.las2peer.services.noracleService.NoracleSpaceService", NoracleService.API_VERSION);
        startService(nodes.get(0), "i5.las2peer.services.noracleService.NoracleQuestionService", NoracleService.API_VERSION);
        startService(nodes.get(0), "i5.las2peer.services.noracleService.NoracleQuestionRelationService", NoracleService.API_VERSION);
        startService(nodes.get(0), "i5.las2peer.services.noracleService.NoracleVoteSe" + "rvice", NoracleService.API_VERSION);
        testAgent = MockAgentFactory.getAdam();
        testAgent.unlock("adamspass");
        PastryNodeImpl activeNode = nodes.get(0);
        activeNode.storeAgent(testAgent);
        basicAuthHeader = "basic " + Base64.getEncoder().encodeToString((testAgent.getLoginName() + ":" + "adamspass").getBytes(StandardCharsets.UTF_8));
        testAgent2 = MockAgentFactory.getEve();
        testAgent2.unlock("evespass");
        activeNode.storeAgent(testAgent2);
        basicAuthHeader2 = "basic " + Base64.getEncoder().encodeToString((testAgent2.getLoginName() + ":" + "evespass").getBytes(StandardCharsets.UTF_8));
        baseUrl = connector.getHttpEndpoint() + "/" + NoracleService.RESOURCE_NAME + "/v" + NoracleService.API_VERSION;
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }
}
Also used : MultiPartFeature(org.glassfish.jersey.media.multipart.MultiPartFeature) WebConnector(i5.las2peer.connectors.webConnector.WebConnector) PastryNodeImpl(i5.las2peer.p2p.PastryNodeImpl) Before(org.junit.Before)

Example 8 with Agent

use of i5.las2peer.api.security.Agent 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 9 with Agent

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

Example 10 with Agent

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

the class NoracleAgentService method updateAgentProfile.

@Override
public NoracleAgentProfile updateAgentProfile(String agentName) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    String envIdentifier = buildAgentProfileId(mainAgent.getIdentifier());
    Envelope env;
    NoracleAgentProfile profile;
    // look for existing profile, otherwise create one
    try {
        try {
            env = Context.get().requestEnvelope(envIdentifier);
            profile = (NoracleAgentProfile) env.getContent();
        } catch (EnvelopeNotFoundException e) {
            env = Context.get().createEnvelope(envIdentifier);
            profile = new NoracleAgentProfile();
        }
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope access denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not create new envelope for noracle agent profile", e);
    }
    profile.setName(agentName);
    env.setContent(profile);
    env.setPublic();
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Envelope access denied");
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Storing envelope with noracle agent profile failed", e);
    }
    return profile;
}
Also used : AnonymousAgent(i5.las2peer.api.security.AnonymousAgent) Agent(i5.las2peer.api.security.Agent) NoracleAgentProfile(i5.las2peer.services.noracleService.model.NoracleAgentProfile) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope)

Aggregations

EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)10 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)10 Envelope (i5.las2peer.api.persistency.Envelope)8 EnvelopeNotFoundException (i5.las2peer.api.persistency.EnvelopeNotFoundException)7 Agent (i5.las2peer.api.security.Agent)6 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)6 SerializationException (i5.las2peer.serialization.SerializationException)3 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)3 SpaceSubscriptionList (i5.las2peer.services.noracleService.model.SpaceSubscriptionList)3 CryptoException (i5.las2peer.tools.CryptoException)3 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)2 UserAgentImpl (i5.las2peer.security.UserAgentImpl)2 NoracleAgentProfile (i5.las2peer.services.noracleService.model.NoracleAgentProfile)2 Space (i5.las2peer.services.noracleService.model.Space)2 Vote (i5.las2peer.services.noracleService.model.Vote)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 WebConnector (i5.las2peer.connectors.webConnector.WebConnector)1