View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling 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. 
3   	$Id
4   */
5   package com.hack23.cia.web.service;
6   
7   import java.io.IOException;
8   
9   import javax.servlet.http.HttpServletRequest;
10  import javax.servlet.http.HttpServletResponse;
11  
12  import org.springframework.util.FileCopyUtils;
13  import org.springframework.web.servlet.ModelAndView;
14  import org.springframework.web.servlet.mvc.AbstractController;
15  
16  import com.hack23.cia.model.application.impl.common.ImageContent;
17  import com.hack23.cia.service.api.application.ApplicationManager;
18  import com.hack23.cia.service.api.common.ImageContentRequest;
19  import com.hack23.cia.service.api.common.ImageContentResponse;
20  import com.hack23.cia.service.api.common.ServiceRequest;
21  import com.hack23.cia.service.api.common.ServiceResponse;
22  import com.hack23.cia.service.api.common.ServiceResponse.ServiceResult;
23  
24  /***
25   * The Class ImageResourcesService.
26   */
27  public class ImageResourcesService extends AbstractController {
28  
29  	/*** The application manager. */
30  	private final ApplicationManager applicationManager;
31  	
32      /***
33       * Instantiates a new image resources service.
34       *
35       * @param applicationManager the application manager
36       */
37      public ImageResourcesService(final ApplicationManager applicationManager) {
38  		super();
39  		this.applicationManager = applicationManager;
40  	}
41  
42  	/* (non-Javadoc)
43       * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
44       */
45      @Override
46      protected final ModelAndView handleRequestInternal(final HttpServletRequest request,
47              final HttpServletResponse response) throws Exception {
48  
49      	loadAndWriteFile(request, response);
50          
51          return null;
52      }
53  
54  	/***
55  	 * Load and write file.
56  	 *
57  	 * @param request the request
58  	 * @param response the response
59  	 * @throws IOException Signals that an I/O exception has occurred.
60  	 */
61  	public final void loadAndWriteFile(final HttpServletRequest request,
62  			final HttpServletResponse response) throws IOException {
63  		final String fileName = request.getParameter("filename");
64      	ImageContent resource = null;
65      	
66      	if (fileName !=null) {
67      		final ServiceRequest serviceRequest = new ImageContentRequest(null, fileName);
68  			final ServiceResponse serviceResponse = applicationManager.service(serviceRequest);
69      		if (ServiceResult.SUCCESS.equals(serviceResponse.getResult())) {    			
70      			final ImageContentResponse imageContentResponse = (ImageContentResponse) serviceResponse;
71      			resource = imageContentResponse.getImageContent();
72      		}			
73      	}
74      		
75      	String filename ="file.png";
76  		byte[] content = "".getBytes();
77  		String mimetype = "image/png";
78  
79      	if (resource != null ) {
80      		filename =resource.getFileName();
81      		content = resource.getImageContent();
82      		mimetype = resource.getMimetype();
83      	}
84          response.setContentLength(content.length);
85          response.setContentType(mimetype);
86          FileCopyUtils.copy(content , response.getOutputStream());
87  	}
88  
89  }