/* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.activiti.rest.service.api.repository; import java.io.InputStream; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.activiti.engine.ActivitiException; import org.activiti.engine.ActivitiIllegalArgumentException; import org.activiti.engine.ActivitiObjectNotFoundException; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import org.activiti.rest.common.application.ContentTypeResolver; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; /** * @author Frederik Heremans */ public class BaseDeploymentResourceDataResource { @Autowired protected ContentTypeResolver contentTypeResolver; @Autowired protected RepositoryService repositoryService; protected byte[] getDeploymentResourceData(String deploymentId, String resourceId, HttpServletResponse response) { if (deploymentId == null) { throw new ActivitiIllegalArgumentException("No deployment id provided"); } if (resourceId == null) { throw new ActivitiIllegalArgumentException("No resource id provided"); } // Check if deployment exists Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult(); if (deployment == null) { throw new ActivitiObjectNotFoundException("Could not find a deployment with id '" + deploymentId + "'.", Deployment.class); } List<String> resourceList = repositoryService.getDeploymentResourceNames(deploymentId); if (resourceList.contains(resourceId)) { final InputStream resourceStream = repositoryService.getResourceAsStream(deploymentId, resourceId); String contentType = contentTypeResolver.resolveContentType(resourceId); response.setContentType(contentType); try { return IOUtils.toByteArray(resourceStream); } catch (Exception e) { throw new ActivitiException("Error converting resource stream", e); } } else { // Resource not found in deployment throw new ActivitiObjectNotFoundException("Could not find a resource with id '" + resourceId + "' in deployment '" + deploymentId + "'.", String.class); } } }