use of i5.las2peer.api.persistency.Envelope 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;
}
use of i5.las2peer.api.persistency.Envelope 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;
}
use of i5.las2peer.api.persistency.Envelope in project Distributed-Noracle-Backend by Distributed-Noracle.
the class NoracleQuestionService method retrieveQuestion.
private boolean retrieveQuestion(QuestionList result, String spaceId, int questionNumber) throws EnvelopeNotFoundException {
try {
Envelope spaceQuestionEnv = Context.get().requestEnvelope(buildSpaceQuestionNumberId(spaceId, questionNumber));
String questionId = (String) spaceQuestionEnv.getContent();
Envelope questionEnv = Context.get().requestEnvelope(getQuestionEnvelopeIdentifier(questionId));
Question question = (Question) questionEnv.getContent();
// TODO check if author is a member of this space?
// String authorId = question.getAuthorId();
// if (authorId == null || authorId.isEmpty()) {
// return false;
// }
result.add(question);
return true;
} catch (EnvelopeNotFoundException e) {
throw e;
} catch (Exception e) {
// XXX logging
}
return false;
}
use of i5.las2peer.api.persistency.Envelope 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;
}
use of i5.las2peer.api.persistency.Envelope 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;
}
Aggregations