KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > web > controller > ProjectContentController


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.web.controller;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36
37 import org.apache.log4j.Logger;
38 import org.openi.project.ProjectContext;
39 import org.springframework.web.servlet.ModelAndView;
40 import org.springframework.web.servlet.mvc.AbstractController;
41
42
43 /**
44  * @author Uddhab Pant <br>
45  *
46  * This controller retrieves project contents and
47  * contructs url for the content so that view can
48  * display the content.
49  *
50  */

51 public class ProjectContentController extends AbstractController {
52     private static Logger logger = Logger.getLogger(ProjectContentController.class);
53
54     /**
55      * Process the request and return a ModelAndView object which the DispatcherServlet will render.
56      *
57      * @param httpServletRequest HttpServletRequest
58      * @param httpServletResponse HttpServletResponse
59      * @return ModelAndView
60      * @throws Exception
61      */

62     protected ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request,
63         HttpServletResponse JavaDoc response) throws Exception JavaDoc {
64         String JavaDoc contentUrl = "";
65
66         Map JavaDoc model;
67         HttpSession JavaDoc session;
68
69         try {
70             model = new HashMap JavaDoc();
71
72             session = request.getSession();
73
74             String JavaDoc content = request.getParameter("content");
75
76             // if content is available
77
ProjectContext projectContext;
78             projectContext = (ProjectContext) session.getAttribute(
79                     "projectContext");
80
81             // TODO: modify menu builder to create the proper link
82
// create DrillThrough controller (.drillthrough
83
// create RdbmsConrtoller (for .jrxml)
84
if (content != null) {
85                 if (content.endsWith(".drillthrough")) {
86                     String JavaDoc filePath = projectContext.getProjectDirectory()
87                         + "/" + content;
88                     writeToOutputStream(filePath, response);
89
90                     return null;
91                 } else {
92                     // contruct content url
93
contentUrl = "../" + request.getContextPath()
94                         + "-projects/"
95                         + projectContext.getProject().getProjectId();
96
97                     contentUrl = contentUrl + "/" + content;
98                 }
99             }
100
101             model.put("content", contentUrl);
102
103             return new ModelAndView("projectContentView", "model", model);
104         } catch (Exception JavaDoc e) {
105             logger.error("Exception:", e);
106             throw e;
107         }
108     }
109
110     /**
111      * TODO: move to DrillthroughController
112      */

113     private void writeToOutputStream(String JavaDoc srcFile,
114         HttpServletResponse JavaDoc response) throws Exception JavaDoc {
115         logger.info("writing file '" + srcFile + "' to response outputstream");
116
117         File JavaDoc file = new File JavaDoc(srcFile);
118
119         //check whether RDBMS process is completed or not
120
if (file.getName().startsWith("still-retrieving-")) {
121             logger.warn("RDBMS processing is not completed yet");
122
123             return;
124         }
125
126         FileInputStream JavaDoc inFile = null;
127
128         try {
129             inFile = new FileInputStream JavaDoc(file);
130         } catch (FileNotFoundException JavaDoc ex1) {
131             throw ex1;
132         }
133
134         response.setContentLength((int) file.length());
135         response.setContentType("text/plain");
136         response.setHeader("Pragma", "no-cache");
137         response.setHeader("Content-Disposition",
138             "attachment; filename=\"" + file.getName() + "\"");
139
140         InputStream JavaDoc in = null;
141         in = new BufferedInputStream JavaDoc(inFile);
142
143         OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(response.getOutputStream());
144         byte[] array = new byte[1024];
145         int i = 0;
146
147         try {
148             while ((i = in.read(array, 0, array.length)) != -1) {
149                 try {
150                     out.write(array, 0, i);
151                 } catch (IOException JavaDoc ex) {
152                     //this is usual and happens when browser get disconnected. so just return;
153
break;
154                 }
155             }
156         } catch (IOException JavaDoc ex2) {
157             throw ex2;
158         } finally {
159             try {
160                 if (in != null) {
161                     in.close();
162                 }
163
164                 if (inFile != null) {
165                     inFile.close();
166                 }
167
168                 //RequestContext.instance().setResponseComplete(true);
169
out.close();
170             } catch (Exception JavaDoc ex) {
171                 logger.warn(ex.getMessage(), ex);
172             }
173         }
174     }
175 }
176
Popular Tags