Search in sources :

Example 11 with ServiceNameVersion

use of i5.las2peer.api.p2p.ServiceNameVersion in project Distributed-Noracle-Backend by Distributed-Noracle.

the class SpacesResource method createSpace.

@Override
public Space createSpace(String name) throws ServiceInvocationException {
    Serializable rmiResult = Context.get().invoke(new ServiceNameVersion(NoracleSpaceService.class.getCanonicalName(), NoracleService.API_VERSION), "createSpace", name);
    Space space;
    if (rmiResult instanceof Space) {
        space = (Space) rmiResult;
    } else {
        throw new InternalServiceException("Unexpected result (" + rmiResult.getClass().getCanonicalName() + ") of RMI call");
    }
    return space;
}
Also used : Space(i5.las2peer.services.noracleService.model.Space) Serializable(java.io.Serializable) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion)

Example 12 with ServiceNameVersion

use of i5.las2peer.api.p2p.ServiceNameVersion in project Distributed-Noracle-Backend by Distributed-Noracle.

the class SpacesResource method createSpace.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
@ApiResponses({ @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "Space successfully created"), @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "You have to be logged in to create a space", response = ExceptionEntity.class), @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal Server Error", response = ExceptionEntity.class) })
public Response createSpace(@ApiParam(required = true) CreateSpacePojo createSpacePojo) throws ServiceInvocationException {
    Space space = createSpace(createSpacePojo.getName());
    try {
        Gson gson = new Gson();
        String spaceJSON = gson.toJson(createSpacePojo);
        JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
        try {
            JSONObject obj = (JSONObject) p.parse(spaceJSON);
            obj.put("uid", Context.getCurrent().getMainAgent().getIdentifier());
            Context.get().monitorEvent(MonitoringEvent.SERVICE_CUSTOM_MESSAGE_9, obj.toJSONString());
        } catch (ParseException e) {
            logger.warning("Problems with monitoring event...");
            e.printStackTrace();
        }
        try {
            SpaceSubscription rmiResult = (SpaceSubscription) Context.get().invoke(new ServiceNameVersion(NoracleAgentService.class.getCanonicalName(), NoracleService.API_VERSION), "subscribeToSpace", space.getSpaceId(), space.getSpaceSecret());
        } catch (Exception ex) {
            logger.warning("Error, while trying to subscribe to space: " + ex.getMessage());
        }
        // TODO: return https instead of http
        URI uri = new URI(null, null, RESOURCE_NAME + "/" + space.getSpaceId(), null);
        Response response = Response.created(uri).build();
        return response;
    } catch (URISyntaxException e) {
        logger.warning("URISyntaxException: " + e.getMessage());
        throw new InternalServerErrorException(e);
    }
}
Also used : Space(i5.las2peer.services.noracleService.model.Space) Gson(com.google.gson.Gson) NoracleAgentService(i5.las2peer.services.noracleService.NoracleAgentService) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ParseException(net.minidev.json.parser.ParseException) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) JSONObject(net.minidev.json.JSONObject) ServiceNameVersion(i5.las2peer.api.p2p.ServiceNameVersion) JSONParser(net.minidev.json.parser.JSONParser) ParseException(net.minidev.json.parser.ParseException) SpaceSubscription(i5.las2peer.services.noracleService.model.SpaceSubscription) ApiResponses(io.swagger.annotations.ApiResponses)

Example 13 with ServiceNameVersion

use of i5.las2peer.api.p2p.ServiceNameVersion 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 14 with ServiceNameVersion

use of i5.las2peer.api.p2p.ServiceNameVersion in project Distributed-Noracle-Backend by Distributed-Noracle.

the class QuestionRelationsResource 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 ServiceNameVersion

use of i5.las2peer.api.p2p.ServiceNameVersion 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)

Aggregations

ServiceNameVersion (i5.las2peer.api.p2p.ServiceNameVersion)16 Serializable (java.io.Serializable)13 InternalServiceException (i5.las2peer.api.execution.InternalServiceException)6 ApiResponses (io.swagger.annotations.ApiResponses)5 Gson (com.google.gson.Gson)3 Space (i5.las2peer.services.noracleService.model.Space)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 JSONObject (net.minidev.json.JSONObject)3 JSONParser (net.minidev.json.parser.JSONParser)3 ParseException (net.minidev.json.parser.ParseException)3 ServiceInvocationException (i5.las2peer.api.execution.ServiceInvocationException)2 Envelope (i5.las2peer.api.persistency.Envelope)2 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)2 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)2 Question (i5.las2peer.services.noracleService.model.Question)2 SpaceSubscription (i5.las2peer.services.noracleService.model.SpaceSubscription)2 Vote (i5.las2peer.services.noracleService.model.Vote)2 VoteList (i5.las2peer.services.noracleService.model.VoteList)2 Instant (java.time.Instant)2