Search in sources :

Example 1 with JAXBContent

use of org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent in project entando-core by entando.

the class ApiContentInterface method addContent.

public StringApiResponse addContent(JAXBContent jaxbContent, Properties properties) throws Throwable {
    StringApiResponse response = new StringApiResponse();
    try {
        String typeCode = jaxbContent.getTypeCode();
        Content prototype = (Content) this.getContentManager().getEntityPrototype(typeCode);
        if (null == prototype) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Content type with code '" + typeCode + "' does not exist", Response.Status.CONFLICT);
        }
        Content content = (Content) jaxbContent.buildEntity(prototype, this.getCategoryManager());
        if (null != content.getId()) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "You cannot specify Content Id", Response.Status.CONFLICT);
        }
        UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
        content.setFirstEditor((null != user) ? user.getUsername() : SystemConstants.GUEST_USER_NAME);
        response = this.validateAndSaveContent(content, properties);
    } catch (ApiException ae) {
        response.addErrors(ae.getErrors());
        response.setResult(IResponseBuilder.FAILURE, null);
    } catch (Throwable t) {
        _logger.error("Error adding content", t);
        throw new ApsSystemException("Error adding content", t);
    }
    return response;
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) Content(com.agiletec.plugins.jacms.aps.system.services.content.model.Content) JAXBContent(org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 2 with JAXBContent

use of org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent in project entando-core by entando.

the class ApiContentInterface method getContent.

public JAXBContent getContent(Properties properties) throws ApiException, Throwable {
    JAXBContent jaxbContent = null;
    String id = properties.getProperty("id");
    try {
        String langCode = properties.getProperty(SystemConstants.API_LANG_CODE_PARAMETER);
        Content mainContent = this.getPublicContent(id);
        jaxbContent = this.getJAXBContentInstance(mainContent, langCode);
        UserDetails user = (UserDetails) properties.get(SystemConstants.API_USER_PARAMETER);
        if (null == user) {
            user = this.getUserManager().getGuestUser();
        }
        if (!this.getContentAuthorizationHelper().isAuth(user, mainContent)) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Required content '" + id + "' does not allowed", Response.Status.FORBIDDEN);
        }
    } catch (ApiException ae) {
        throw ae;
    } catch (Throwable t) {
        _logger.error("error in getContent", t);
        throw new ApsSystemException("Error into API method", t);
    }
    return jaxbContent;
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) Content(com.agiletec.plugins.jacms.aps.system.services.content.model.Content) JAXBContent(org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) JAXBContent(org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 3 with JAXBContent

use of org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent in project entando-core by entando.

the class ApiContentInterface method updateContent.

public StringApiResponse updateContent(JAXBContent jaxbContent, Properties properties) throws Throwable {
    StringApiResponse response = new StringApiResponse();
    try {
        String typeCode = jaxbContent.getTypeCode();
        Content prototype = (Content) this.getContentManager().getEntityPrototype(typeCode);
        if (null == prototype) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Content type with code '" + typeCode + "' does not exist", Response.Status.CONFLICT);
        }
        Content content = (Content) jaxbContent.buildEntity(prototype, this.getCategoryManager());
        Content masterContent = this.getContentManager().loadContent(content.getId(), false);
        if (null == masterContent) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Content with code '" + content.getId() + "' does not exist", Response.Status.CONFLICT);
        } else if (!masterContent.getMainGroup().equals(content.getMainGroup())) {
            throw new ApiException(IApiErrorCodes.API_VALIDATION_ERROR, "Invalid main group " + content.getMainGroup() + " not equals then master " + masterContent.getMainGroup(), Response.Status.CONFLICT);
        }
        response = this.validateAndSaveContent(content, properties);
    } catch (ApiException ae) {
        response.addErrors(ae.getErrors());
        response.setResult(IResponseBuilder.FAILURE, null);
    } catch (Throwable t) {
        _logger.error("Error updating content", t);
        throw new ApsSystemException("Error updating content", t);
    }
    return response;
}
Also used : Content(com.agiletec.plugins.jacms.aps.system.services.content.model.Content) JAXBContent(org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Example 4 with JAXBContent

use of org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent in project entando-core by entando.

the class TestApiContentInterface method testGetContent.

protected JAXBContent testGetContent(MediaType mediaType, String username, String contentId, String langCode) throws Throwable {
    ApiResource contentResource = this.getApiCatalogManager().getResource("jacms", "content");
    ApiMethod getMethod = contentResource.getGetMethod();
    Properties properties = super.createApiProperties(username, langCode, mediaType);
    properties.put("id", contentId);
    Object result = this.getResponseBuilder().createResponse(getMethod, properties);
    assertNotNull(result);
    ApiContentInterface apiContentInterface = (ApiContentInterface) this.getApplicationContext().getBean("jacmsApiContentInterface");
    Object singleResult = apiContentInterface.getContent(properties);
    assertNotNull(singleResult);
    String toString = this.marshall(singleResult, mediaType);
    InputStream stream = new ByteArrayInputStream(toString.getBytes());
    JAXBContent jaxbContent = (JAXBContent) UnmarshalUtils.unmarshal(super.getApplicationContext(), JAXBContent.class, stream, mediaType);
    assertNotNull(jaxbContent);
    return jaxbContent;
}
Also used : ApiResource(org.entando.entando.aps.system.services.api.model.ApiResource) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) Properties(java.util.Properties) JAXBContent(org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent)

Example 5 with JAXBContent

use of org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent in project entando-core by entando.

the class TestApiContentInterface method testCreateNewContent.

protected void testCreateNewContent(MediaType mediaType, String contentId) throws Throwable {
    String dateNow = DateConverter.getFormattedDate(new Date(), JacmsSystemConstants.CONTENT_METADATA_DATE_FORMAT);
    EntitySearchFilter filter = new EntitySearchFilter(IContentManager.CONTENT_CREATION_DATE_FILTER_KEY, false, dateNow, null);
    EntitySearchFilter[] filters = { filter };
    List<String> ids = this._contentManager.searchId(filters);
    assertTrue(ids.isEmpty());
    JAXBContent jaxbContent = this.testGetContent(mediaType, "admin", contentId, "it");
    ApiResource contentResource = this.getApiCatalogManager().getResource("jacms", "content");
    ApiMethod postMethod = contentResource.getPostMethod();
    Properties properties = super.createApiProperties("admin", "it", mediaType);
    try {
        jaxbContent.setId(null);
        Object response = this.getResponseBuilder().createResponse(postMethod, jaxbContent, properties);
        assertNotNull(response);
        assertTrue(response instanceof StringApiResponse);
        assertEquals(IResponseBuilder.SUCCESS, ((StringApiResponse) response).getResult());
        ids = this._contentManager.searchId(filters);
        assertEquals(1, ids.size());
        String newContentId = ids.get(0);
        Content newContent = this._contentManager.loadContent(newContentId, false);
        Content masterContent = this._contentManager.loadContent(contentId, true);
        List<AttributeInterface> attributes = masterContent.getAttributeList();
        for (int i = 0; i < attributes.size(); i++) {
            AttributeInterface attribute = attributes.get(i);
            AttributeInterface newAttribute = (AttributeInterface) newContent.getAttribute(attribute.getName());
            this.checkAttributes(attribute, newAttribute);
        }
    } catch (Exception e) {
        throw e;
    } finally {
        ids = this._contentManager.searchId(filters);
        if (!ids.isEmpty()) {
            for (int i = 0; i < ids.size(); i++) {
                String id = ids.get(i);
                Content content = this._contentManager.loadContent(id, false);
                this._contentManager.deleteContent(content);
            }
        }
    }
}
Also used : ApiResource(org.entando.entando.aps.system.services.api.model.ApiResource) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) Properties(java.util.Properties) Date(java.util.Date) Content(com.agiletec.plugins.jacms.aps.system.services.content.model.Content) JAXBContent(org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent) JAXBContent(org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent) EntitySearchFilter(com.agiletec.aps.system.common.entity.model.EntitySearchFilter) StringApiResponse(org.entando.entando.aps.system.services.api.model.StringApiResponse) AttributeInterface(com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)

Aggregations

JAXBContent (org.entando.entando.plugins.jacms.aps.system.services.api.model.JAXBContent)5 Content (com.agiletec.plugins.jacms.aps.system.services.content.model.Content)4 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)3 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)3 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)3 UserDetails (com.agiletec.aps.system.services.user.UserDetails)2 Properties (java.util.Properties)2 ApiMethod (org.entando.entando.aps.system.services.api.model.ApiMethod)2 ApiResource (org.entando.entando.aps.system.services.api.model.ApiResource)2 EntitySearchFilter (com.agiletec.aps.system.common.entity.model.EntitySearchFilter)1 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1