Search in sources :

Example 1 with ResourceInstance

use of com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance in project entando-core by entando.

the class ProtectedResourceProvider method provideProtectedResource.

@Override
public boolean provideProtectedResource(HttpServletRequest request, HttpServletResponse response) throws ApsSystemException {
    try {
        String[] uriSegments = request.getRequestURI().split("/");
        int segments = uriSegments.length;
        // CONTROLLO ASSOCIAZIONE RISORSA A CONTENUTO
        int indexGuardian = 0;
        String checkContentAssociation = uriSegments[segments - 2];
        if (checkContentAssociation.equals(AbstractResourceAttribute.REFERENCED_RESOURCE_INDICATOR)) {
            // LA Sintassi /<RES_ID>/<SIZE>/<LANG_CODE>/<REFERENCED_RESOURCE_INDICATOR>/<CONTENT_ID>
            indexGuardian = 2;
        }
        String resId = uriSegments[segments - 3 - indexGuardian];
        UserDetails currentUser = (UserDetails) request.getSession().getAttribute(SystemConstants.SESSIONPARAM_CURRENT_USER);
        if (currentUser == null) {
            currentUser = this.getUserManager().getGuestUser();
        }
        boolean isAuthForProtectedRes = false;
        if (indexGuardian != 0) {
            if (this.isAuthOnProtectedRes(currentUser, resId, uriSegments[segments - 1])) {
                isAuthForProtectedRes = true;
            } else {
                this.executeLoginRedirect(request, response);
                return true;
            }
        }
        ResourceInterface resource = this.getResourceManager().loadResource(resId);
        if (resource == null) {
            return false;
        }
        IAuthorizationManager authManager = this.getAuthorizationManager();
        if (isAuthForProtectedRes || authManager.isAuthOnGroup(currentUser, resource.getMainGroup()) || authManager.isAuthOnGroup(currentUser, Group.ADMINS_GROUP_NAME)) {
            ResourceInstance instance = null;
            if (resource.isMultiInstance()) {
                String sizeStr = uriSegments[segments - 2 - indexGuardian];
                if (!this.isValidNumericString(sizeStr)) {
                    return false;
                }
                int size = Integer.parseInt(sizeStr);
                String langCode = uriSegments[segments - 1 - indexGuardian];
                instance = ((AbstractMultiInstanceResource) resource).getInstance(size, langCode);
            } else {
                instance = ((AbstractMonoInstanceResource) resource).getInstance();
            }
            this.createResponse(response, resource, instance);
            return true;
        }
    } catch (Throwable t) {
        _logger.error("Error extracting protected resource", t);
        throw new ApsSystemException("Error extracting protected resource", t);
    }
    return false;
}
Also used : IAuthorizationManager(com.agiletec.aps.system.services.authorization.IAuthorizationManager) UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResourceInstance(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ResourceInterface(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface)

Example 2 with ResourceInstance

use of com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance in project entando-core by entando.

the class AbstractImageResizer method saveResizedImage.

@Override
public ResourceInstance saveResizedImage(String subPath, boolean isProtectedResource, ImageIcon imageIcon, ImageResourceDimension dimension) throws ApsSystemException {
    ResourceInstance resizedInstance = new ResourceInstance();
    resizedInstance.setSize(dimension.getIdDim());
    BufferedImage outImage = this.getResizedImage(imageIcon, dimension.getDimx(), dimension.getDimy());
    String filename = subPath.substring(subPath.lastIndexOf("/") + 1);
    resizedInstance.setFileName(filename);
    String tempFilePath = System.getProperty("java.io.tmpdir") + File.separator + "temp_" + filename;
    try {
        File tempFile = new File(tempFilePath);
        ImageIO.write(outImage, this.getFileExtension(tempFilePath), tempFile);
        this.getStorageManager().saveFile(subPath, isProtectedResource, new FileInputStream(tempFile));
        // resizedInstance.setMimeType(bean.getMimeType());
        long realLength = tempFile.length() / 1000;
        resizedInstance.setFileLength(String.valueOf(realLength) + " Kb");
        tempFile.delete();
    } catch (Throwable t) {
        _logger.error("Error creating resized Image", t);
        String msg = "Error creating resigned Image";
        // ApsSystemUtils.logThrowable(t, this, "saveImageResized", msg);
        throw new ApsSystemException(msg, t);
    }
    String mimeType = URLConnection.guessContentTypeFromName(filename);
    resizedInstance.setMimeType(mimeType);
    return resizedInstance;
}
Also used : ResourceInstance(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) FileInputStream(java.io.FileInputStream)

Example 3 with ResourceInstance

use of com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance in project entando-core by entando.

the class ResourceManagerIntegrationTest method testAddRemoveImageResource.

private void testAddRemoveImageResource(String mainGroup) throws Throwable {
    List<String> allowedGroups = this.getAllGroupCodes();
    ResourceInterface res = null;
    String resDescrToAdd = "Entando Logo";
    String resourceType = "Image";
    String categoryCodeToAdd = "resCat1";
    ResourceDataBean bean = this.getMockResource(resourceType, mainGroup, resDescrToAdd, categoryCodeToAdd);
    try {
        List<String> resourcesId = _resourceManager.searchResourcesId(resourceType, null, categoryCodeToAdd, allowedGroups);
        assertEquals(1, resourcesId.size());
        this._resourceManager.addResource(bean);
        resourcesId = _resourceManager.searchResourcesId(resourceType, resDescrToAdd, null, allowedGroups);
        assertEquals(resourcesId.size(), 1);
        resourcesId = _resourceManager.searchResourcesId(resourceType, resDescrToAdd, categoryCodeToAdd, allowedGroups);
        assertEquals(resourcesId.size(), 1);
        res = this._resourceManager.loadResource(resourcesId.get(0));
        assertTrue(res instanceof ImageResource);
        assertEquals(res.getCategories().size(), 1);
        assertEquals(res.getDescription(), resDescrToAdd);
        ResourceInstance instance0 = ((ImageResource) res).getInstance(0, null);
        assertEquals("entando_logo.jpg", res.getMasterFileName());
        assertEquals("image/jpeg", instance0.getMimeType());
        resourcesId = _resourceManager.searchResourcesId(resourceType, null, categoryCodeToAdd, allowedGroups);
        assertEquals(resourcesId.size(), 2);
    } catch (Throwable t) {
        throw t;
    } finally {
        if (res != null) {
            this._resourceManager.deleteResource(res);
            List<String> resources = _resourceManager.searchResourcesId(resourceType, resDescrToAdd, null, allowedGroups);
            assertEquals(resources.size(), 0);
            resources = _resourceManager.searchResourcesId(resourceType, null, categoryCodeToAdd, allowedGroups);
            assertEquals(resources.size(), 1);
        }
    }
}
Also used : ImageResource(com.agiletec.plugins.jacms.aps.system.services.resource.model.ImageResource) MockResourceDataBean(com.agiletec.plugins.jacms.aps.system.services.resource.mock.MockResourceDataBean) ResourceDataBean(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceDataBean) ResourceInstance(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance) ResourceInterface(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface)

Example 4 with ResourceInstance

use of com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance in project entando-core by entando.

the class ResourceManager method refreshMasterFileNames.

protected void refreshMasterFileNames(String resourceId) {
    try {
        ResourceInterface resource = this.loadResource(resourceId);
        if (resource.isMultiInstance()) {
            ResourceInstance instance = ((AbstractMultiInstanceResource) resource).getInstance(0, null);
            String filename = instance.getFileName();
            int index = filename.lastIndexOf("_d0.");
            String masterFileName = filename.substring(0, index) + filename.substring(index + 3);
            resource.setMasterFileName(masterFileName);
        } else {
            ResourceInstance instance = ((AbstractMonoInstanceResource) resource).getInstance();
            resource.setMasterFileName(instance.getFileName());
        }
        this.updateResource(resource);
    } catch (Throwable t) {
        logger.error("Error reloading master file name of resource {}", resourceId, t);
    }
}
Also used : ResourceInstance(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance) ResourceInterface(com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface) AbstractMultiInstanceResource(com.agiletec.plugins.jacms.aps.system.services.resource.model.AbstractMultiInstanceResource) AbstractMonoInstanceResource(com.agiletec.plugins.jacms.aps.system.services.resource.model.AbstractMonoInstanceResource)

Aggregations

ResourceInstance (com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInstance)4 ResourceInterface (com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceInterface)3 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)2 IAuthorizationManager (com.agiletec.aps.system.services.authorization.IAuthorizationManager)1 UserDetails (com.agiletec.aps.system.services.user.UserDetails)1 MockResourceDataBean (com.agiletec.plugins.jacms.aps.system.services.resource.mock.MockResourceDataBean)1 AbstractMonoInstanceResource (com.agiletec.plugins.jacms.aps.system.services.resource.model.AbstractMonoInstanceResource)1 AbstractMultiInstanceResource (com.agiletec.plugins.jacms.aps.system.services.resource.model.AbstractMultiInstanceResource)1 ImageResource (com.agiletec.plugins.jacms.aps.system.services.resource.model.ImageResource)1 ResourceDataBean (com.agiletec.plugins.jacms.aps.system.services.resource.model.ResourceDataBean)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1