Search in sources :

Example 6 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project qi4j-sdk by Qi4j.

the class EntityResource method delete.

@Override
protected Representation delete(Variant variant) throws ResourceException {
    Usecase usecase = UsecaseBuilder.newUsecase("Remove entity");
    EntityStoreUnitOfWork uow = entityStore.newUnitOfWork(usecase, module, System.currentTimeMillis());
    try {
        EntityReference identityRef = EntityReference.parseEntityReference(identity);
        uow.entityStateOf(identityRef).remove();
        uow.applyChanges().commit();
        getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
    } catch (EntityNotFoundException e) {
        uow.discard();
        getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
    }
    return new EmptyRepresentation();
}
Also used : EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) EntityReference(org.qi4j.api.entity.EntityReference) Usecase(org.qi4j.api.usecase.Usecase) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException)

Example 7 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project qi4j-sdk by Qi4j.

the class EntityResource method post.

@Override
public Representation post(Representation entityRepresentation, Variant variant) throws ResourceException {
    Usecase usecase = UsecaseBuilder.newUsecase("Update entity");
    EntityStoreUnitOfWork unitOfWork = entityStore.newUnitOfWork(usecase, module, System.currentTimeMillis());
    EntityState entity = getEntityState(unitOfWork);
    Form form = new Form(entityRepresentation);
    try {
        final EntityDescriptor descriptor = entity.entityDescriptor();
        // Parse JSON into properties
        for (PropertyDescriptor persistentProperty : descriptor.state().properties()) {
            if (!persistentProperty.isImmutable()) {
                String formValue = form.getFirstValue(persistentProperty.qualifiedName().name(), null);
                if (formValue == null) {
                    entity.setPropertyValue(persistentProperty.qualifiedName(), null);
                } else {
                    entity.setPropertyValue(persistentProperty.qualifiedName(), valueSerialization.deserialize(persistentProperty.valueType(), formValue));
                }
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().associations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            if (newStringAssociation == null || newStringAssociation.isEmpty()) {
                entity.setAssociationValue(associationType.qualifiedName(), null);
            } else {
                entity.setAssociationValue(associationType.qualifiedName(), EntityReference.parseEntityReference(newStringAssociation));
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().manyAssociations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            ManyAssociationState manyAssociation = entity.manyAssociationValueOf(associationType.qualifiedName());
            if (newStringAssociation == null) {
                // Remove "left-overs"
                for (EntityReference entityReference : manyAssociation) {
                    manyAssociation.remove(entityReference);
                }
                continue;
            }
            BufferedReader bufferedReader = new BufferedReader(new StringReader(newStringAssociation));
            String identity;
            try {
                // Synchronize old and new association
                int index = 0;
                while ((identity = bufferedReader.readLine()) != null) {
                    EntityReference reference = new EntityReference(identity);
                    if (manyAssociation.count() < index && manyAssociation.get(index).equals(reference)) {
                        continue;
                    }
                    try {
                        unitOfWork.entityStateOf(reference);
                        manyAssociation.remove(reference);
                        manyAssociation.add(index++, reference);
                    } catch (EntityNotFoundException e) {
                    // Ignore this entity - doesn't exist
                    }
                }
                // Remove "left-overs"
                while (manyAssociation.count() > index) {
                    manyAssociation.remove(manyAssociation.get(index));
                }
            } catch (IOException e) {
            // Ignore
            }
        }
        for (AssociationDescriptor associationType : descriptor.state().namedAssociations()) {
            String newStringAssociation = form.getFirstValue(associationType.qualifiedName().name());
            NamedAssociationState namedAssociation = entity.namedAssociationValueOf(associationType.qualifiedName());
            if (newStringAssociation == null) {
                // Remove "left-overs"
                for (String name : namedAssociation) {
                    namedAssociation.remove(name);
                }
                continue;
            }
            Set<String> names = new HashSet<>();
            BufferedReader bufferedReader = new BufferedReader(new StringReader(newStringAssociation));
            String line;
            try {
                while ((line = bufferedReader.readLine()) != null) {
                    String name = line;
                    line = bufferedReader.readLine();
                    if (line == null) {
                        break;
                    }
                    String identity = line;
                    EntityReference reference = new EntityReference(identity);
                    try {
                        unitOfWork.entityStateOf(reference);
                        namedAssociation.remove(name);
                        namedAssociation.put(name, reference);
                        names.add(name);
                    } catch (EntityNotFoundException e) {
                    // Ignore this entity - doesn't exist
                    }
                }
                // Remove "left-overs"
                for (String assocName : Iterables.toList(namedAssociation)) {
                    if (!names.contains(assocName)) {
                        namedAssociation.remove(assocName);
                    }
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    } catch (ValueSerializationException | IllegalArgumentException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    }
    try {
        unitOfWork.applyChanges().commit();
    } catch (ConcurrentEntityStateModificationException e) {
        throw new ResourceException(Status.CLIENT_ERROR_CONFLICT);
    } catch (EntityNotFoundException e) {
        throw new ResourceException(Status.CLIENT_ERROR_GONE);
    }
    getResponse().setStatus(Status.SUCCESS_RESET_CONTENT);
    return new EmptyRepresentation();
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork) Form(org.restlet.data.Form) NamedAssociationState(org.qi4j.spi.entity.NamedAssociationState) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) EntityState(org.qi4j.spi.entity.EntityState) JSONEntityState(org.qi4j.spi.entitystore.helpers.JSONEntityState) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) IOException(java.io.IOException) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) EntityReference(org.qi4j.api.entity.EntityReference) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ResourceException(org.restlet.resource.ResourceException) Usecase(org.qi4j.api.usecase.Usecase) HashSet(java.util.HashSet)

Example 8 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project qi4j-sdk by Qi4j.

the class ContextResourceClient method invokeCommand.

private HandlerCommand invokeCommand(Reference ref, Method method, Object requestObject, ResponseHandler responseHandler, ResponseHandler processingErrorHandler) {
    Request request = new Request(method, ref);
    if (requestObject == null)
        requestObject = new EmptyRepresentation();
    contextResourceFactory.writeRequest(request, requestObject);
    contextResourceFactory.updateCommandRequest(request);
    User user = request.getClientInfo().getUser();
    if (user != null)
        request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC, user.getName(), user.getSecret()));
    Response response = new Response(request);
    contextResourceFactory.getClient().handle(request, response);
    try {
        if (response.getStatus().isSuccess()) {
            contextResourceFactory.updateCache(response);
            if (responseHandler != null)
                return responseHandler.handleResponse(response, this);
        } else {
            if (response.getStatus().equals(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY) && processingErrorHandler != null) {
                return processingErrorHandler.handleResponse(response, this);
            } else {
                // TODO This needs to be expanded to allow custom handling of all the various cases
                return errorHandler.handleResponse(response, this);
            }
        }
        // No handler found
        return null;
    } finally {
        try {
            response.getEntity().exhaust();
        } catch (Throwable e) {
        // Ignore
        }
    }
}
Also used : ChallengeResponse(org.restlet.data.ChallengeResponse) Response(org.restlet.Response) User(org.restlet.security.User) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) Request(org.restlet.Request) ChallengeResponse(org.restlet.data.ChallengeResponse)

Example 9 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project qi4j-sdk by Qi4j.

the class LoginResource method login.

public Representation login(String name, String password) {
    context(Login.class).login(name, password);
    EmptyRepresentation rep = new EmptyRepresentation();
    Response.getCurrent().getCookieSettings().add("user", name);
    return rep;
}
Also used : EmptyRepresentation(org.restlet.representation.EmptyRepresentation) Login(org.qi4j.samples.forum.context.login.Login)

Example 10 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project OpenAM by OpenRock.

the class TokenEndpointResourceTest method testToken.

@Test
public void testToken() throws Exception {
    //Given
    Context context = new Context();
    Request request = new Request();
    Response response = new Response(request);
    tokenEndpointResource.init(context, request, response);
    doReturn(new AccessToken(null, OAUTH_ACCESS_TOKEN, null)).when(accessTokenService).requestAccessToken(any(OAuth2Request.class));
    //When
    tokenEndpointResource.token(new EmptyRepresentation());
    //Then
    verify(hook).afterTokenHandling(any(OAuth2Request.class), eq(request), eq(response));
}
Also used : Context(org.restlet.Context) Response(org.restlet.Response) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) AccessToken(org.forgerock.oauth2.core.AccessToken) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Test(org.testng.annotations.Test)

Aggregations

EmptyRepresentation (org.restlet.representation.EmptyRepresentation)10 Request (org.restlet.Request)3 Test (org.testng.annotations.Test)3 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)2 EntityReference (org.qi4j.api.entity.EntityReference)2 Usecase (org.qi4j.api.usecase.Usecase)2 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)2 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)2 Response (org.restlet.Response)2 ChallengeResponse (org.restlet.data.ChallengeResponse)2 Form (org.restlet.data.Form)2 ByteArrayRepresentation (org.restlet.representation.ByteArrayRepresentation)2 FileRepresentation (org.restlet.representation.FileRepresentation)2 InputRepresentation (org.restlet.representation.InputRepresentation)2 Representation (org.restlet.representation.Representation)2 StringRepresentation (org.restlet.representation.StringRepresentation)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1