Search in sources :

Example 11 with Space

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

the class NoracleSpaceService method joinSpace.

public void joinSpace(String spaceId, String spaceSecret) throws ServiceInvocationException {
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    } else if (spaceSecret == null || spaceSecret.isEmpty()) {
        throw new ServiceAccessDeniedException("No space secret given");
    }
    try {
        UserAgentImpl inviteAgent = this.getInviteAgent(spaceId);
        inviteAgent.unlock(spaceSecret);
        GroupAgent memberAgent = this.getMemberAgent(spaceId);
        memberAgent.unlock(inviteAgent);
        memberAgent.addMember(Context.get().getMainAgent());
        Context.get().storeAgent(memberAgent);
    } catch (AgentAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Could not unlock agent", e);
    } catch (Exception e) {
        throw new ServiceInvocationException("Could not join space", e);
    }
}
Also used : UserAgentImpl(i5.las2peer.security.UserAgentImpl) 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 Space

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

the class NoracleSpaceService method getSpace.

@Override
public Space getSpace(String spaceId) throws ServiceInvocationException {
    if (spaceId == null || spaceId.isEmpty()) {
        throw new InvocationBadArgumentException("No space id given");
    }
    Envelope env;
    try {
        env = Context.get().requestEnvelope(getSpaceEnvelopeIdentifier(spaceId));
    } catch (EnvelopeAccessDeniedException e) {
        throw new ServiceAccessDeniedException("Access Denied", e);
    } catch (EnvelopeNotFoundException e) {
        throw new ResourceNotFoundException("Space Not Found", e);
    } catch (EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not deserialize space object", e);
    }
    Space space = (Space) env.getContent();
    return space;
}
Also used : Space(i5.las2peer.services.noracleService.model.Space) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) EnvelopeNotFoundException(i5.las2peer.api.persistency.EnvelopeNotFoundException) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope)

Example 13 with Space

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

the class NoracleSpaceService method createSpace.

@Override
public Space createSpace(String name) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceNotAuthorizedException("You have to be logged in to create a space");
    }
    String spaceId = buildSpaceId();
    String spaceSecret = generateSpaceSecret();
    SpaceInviteAgent spaceInviteAgent;
    try {
        spaceInviteAgent = new SpaceInviteAgent(spaceSecret);
        spaceInviteAgent.unlock(spaceSecret);
        Context.get().storeAgent(spaceInviteAgent);
    } catch (AgentOperationFailedException | CryptoException e) {
        throw new InternalServiceException("Could not create space invite agent", e);
    } catch (AgentAccessDeniedException | AgentAlreadyExistsException | AgentLockedException e) {
        throw new InternalServiceException("Could not store space invite agent", e);
    }
    try {
        Envelope envInviteMapping = Context.get().createEnvelope(getInviteMappingIdentifier(spaceId));
        envInviteMapping.setPublic();
        envInviteMapping.setContent(spaceInviteAgent.getIdentifier());
        Context.get().storeEnvelope(envInviteMapping);
    } catch (EnvelopeOperationFailedException | EnvelopeAccessDeniedException e) {
        throw new InternalServiceException("Could not store space invite mapping", e);
    }
    GroupAgentImpl spaceMemberGroupAgent;
    try {
        spaceMemberGroupAgent = GroupAgentImpl.createGroupAgent(new Agent[] { spaceInviteAgent, mainAgent });
        spaceMemberGroupAgent.unlock(mainAgent);
        Context.get().storeAgent(spaceMemberGroupAgent);
    } catch (AgentOperationFailedException | CryptoException | SerializationException e) {
        throw new InternalServiceException("Could not create space member group agent", e);
    } catch (AgentAccessDeniedException | AgentAlreadyExistsException | AgentLockedException e) {
        throw new InternalServiceException("Could not store space member group agent", e);
    }
    try {
        Envelope envGroupMapping = Context.get().createEnvelope(getMemberMappingIdentifier(spaceId));
        envGroupMapping.setPublic();
        envGroupMapping.setContent(spaceMemberGroupAgent.getIdentifier());
        Context.get().storeEnvelope(envGroupMapping);
    } catch (EnvelopeOperationFailedException | EnvelopeAccessDeniedException e) {
        throw new InternalServiceException("Could not store space group member mapping", e);
    }
    Envelope env;
    try {
        env = Context.get().createEnvelope(getSpaceEnvelopeIdentifier(spaceId), mainAgent);
    } catch (EnvelopeOperationFailedException | EnvelopeAccessDeniedException e) {
        throw new InternalServiceException("Could not create envelope for space", e);
    }
    env.addReader(spaceMemberGroupAgent);
    Space space = new Space(spaceId, spaceSecret, name, mainAgent.getIdentifier(), spaceMemberGroupAgent.getIdentifier());
    env.setContent(space);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException | EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store space envelope", e);
    }
    return space;
}
Also used : Space(i5.las2peer.services.noracleService.model.Space) SpaceInviteAgent(i5.las2peer.services.noracleService.model.SpaceInviteAgent) SerializationException(i5.las2peer.serialization.SerializationException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) SpaceInviteAgent(i5.las2peer.services.noracleService.model.SpaceInviteAgent) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) GroupAgentImpl(i5.las2peer.security.GroupAgentImpl) CryptoException(i5.las2peer.tools.CryptoException)

Example 14 with Space

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

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

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)8 ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)6 Space (i5.las2peer.services.noracleService.model.Space)5 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)4 Serializable (java.io.Serializable)4 Agent (i5.las2peer.api.security.Agent)3 AnonymousAgent (i5.las2peer.api.security.AnonymousAgent)3 SerializationException (i5.las2peer.serialization.SerializationException)3 SpaceSubscriptionList (i5.las2peer.services.noracleService.model.SpaceSubscriptionList)3 CryptoException (i5.las2peer.tools.CryptoException)3 ApiResponses (io.swagger.annotations.ApiResponses)3 UserAgentImpl (i5.las2peer.security.UserAgentImpl)2 Question (i5.las2peer.services.noracleService.model.Question)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 Gson (com.google.gson.Gson)1 GroupAgentImpl (i5.las2peer.security.GroupAgentImpl)1 NoracleAgentService (i5.las2peer.services.noracleService.NoracleAgentService)1