- Timestamp:
- 07/20/10 15:00:32 (22 months ago)
- Location:
- print/trunk/src/main/java/org/mapfish/print
- Files:
-
- 2 modified
-
ShellMapPrinter.java (modified) (1 diff)
-
servlet/MapPrinterServlet.java (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
print/trunk/src/main/java/org/mapfish/print/ShellMapPrinter.java
r3397 r3591 90 90 public void run() throws IOException, JSONException, DocumentException { 91 91 final OutputStream outFile = getOutputStream(); 92 if (clientConfig) { 93 final OutputStreamWriter writer = new OutputStreamWriter(outFile, Charset.forName("UTF-8")); 94 JSONWriter json = new JSONWriter(writer); 95 json.object(); 96 { 97 printer.printClientConfig(json); 92 try { 93 if (clientConfig) { 94 final OutputStreamWriter writer = new OutputStreamWriter(outFile, Charset.forName("UTF-8")); 95 JSONWriter json = new JSONWriter(writer); 96 json.object(); 97 { 98 printer.printClientConfig(json); 99 } 100 json.endObject(); 101 102 writer.close(); 103 104 } else { 105 final InputStream inFile = getInputStream(); 106 printer.print(FileUtilities.readWholeTextStream(inFile, "UTF-8"), outFile, referer); 98 107 } 99 json.endObject(); 100 101 writer.close(); 102 103 } else { 104 final InputStream inFile = getInputStream(); 105 printer.print(FileUtilities.readWholeTextStream(inFile, "UTF-8"), outFile, referer); 108 } finally { 109 outFile.close(); 110 printer.stop(); 106 111 } 107 printer.stop();108 112 } 109 113 -
print/trunk/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java
r3397 r3591 155 155 final String id = generateId(tempFile); 156 156 httpServletResponse.setContentType("application/json; charset=utf-8"); 157 try { 158 final PrintWriter writer = httpServletResponse.getWriter(); 157 PrintWriter writer = null; 158 try { 159 writer = httpServletResponse.getWriter(); 159 160 JSONWriter json = new JSONWriter(writer); 160 161 json.object(); … … 169 170 deleteFile(tempFile); 170 171 throw new ServletException(e); 172 } finally { 173 if(writer != null) { 174 writer.close(); 175 } 171 176 } 172 177 addTempFile(tempFile, id); … … 181 186 protected String getSpecFromPostBody(HttpServletRequest httpServletRequest) throws IOException { 182 187 BufferedReader data = httpServletRequest.getReader(); 183 StringBuilder spec = new StringBuilder(); 184 String cur; 185 while ((cur = data.readLine()) != null) { 186 spec.append(cur).append("\n"); 187 } 188 return spec.toString(); 188 try { 189 StringBuilder spec = new StringBuilder(); 190 String cur; 191 while ((cur = data.readLine()) != null) { 192 spec.append(cur).append("\n"); 193 } 194 return spec.toString(); 195 } finally { 196 if(data != null) { 197 data.close(); 198 } 199 } 189 200 } 190 201 191 202 /** 192 203 * To get the PDF created previously. 193 * @param httpServletRequest194 204 */ 195 205 protected void getPDF(HttpServletRequest req, HttpServletResponse httpServletResponse, String id) throws IOException { … … 213 223 final PrintWriter writer = resp.getWriter(); 214 224 215 final String var = req.getParameter("var"); 216 if (var != null) { 217 writer.print(var + "="); 218 } 219 220 JSONWriter json = new JSONWriter(writer); 221 try { 222 json.object(); 223 { 224 printer.printClientConfig(json); 225 json.key("printURL").value(basePath + PRINT_URL); 226 json.key("createURL").value(basePath + CREATE_URL); 227 } 228 json.endObject(); 229 } catch (JSONException e) { 230 throw new ServletException(e); 231 } 232 if (var != null) { 233 writer.print(";"); 234 } 235 writer.close(); 225 try { 226 final String var = req.getParameter("var"); 227 if (var != null) { 228 writer.print(var + "="); 229 } 230 231 JSONWriter json = new JSONWriter(writer); 232 try { 233 json.object(); 234 { 235 printer.printClientConfig(json); 236 json.key("printURL").value(basePath + PRINT_URL); 237 json.key("createURL").value(basePath + CREATE_URL); 238 } 239 json.endObject(); 240 } catch (JSONException e) { 241 throw new ServletException(e); 242 } 243 if (var != null) { 244 writer.print(";"); 245 } 246 } finally { 247 writer.close(); 248 } 236 249 } 237 250 … … 248 261 //create a temporary file that will contain the PDF 249 262 TempFile tempFile = new TempFile(File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX, getTempDir())); 250 try {251 FileOutputStream out = new FileOutputStream(tempFile);252 263 FileOutputStream out = null; 264 try { 265 out = new FileOutputStream(tempFile); 253 266 getMapPrinter().print(spec, out, referer); 254 out.close(); 267 255 268 return tempFile; 256 269 } catch (IOException e) { … … 263 276 deleteFile(tempFile); 264 277 throw e; 278 } finally { 279 if (out != null) 280 out.close(); 265 281 } 266 282 } … … 272 288 FileInputStream pdf = new FileInputStream(tempFile); 273 289 final OutputStream response = httpServletResponse.getOutputStream(); 290 try { 274 291 httpServletResponse.setContentType("application/pdf"); 275 292 if (inline != true) { … … 277 294 } 278 295 FileUtilities.copyStream(pdf, response); 279 pdf.close(); 280 response.close(); 296 } finally { 297 try { 298 pdf.close(); 299 } finally{ 300 response.close(); 301 } 302 } 281 303 } 282 304 … … 285 307 */ 286 308 protected void error(HttpServletResponse httpServletResponse, Throwable e) { 309 PrintWriter out = null; 287 310 try { 288 311 httpServletResponse.setContentType("text/plain"); 289 312 httpServletResponse.setStatus(500); 290 PrintWriterout = httpServletResponse.getWriter();313 out = httpServletResponse.getWriter(); 291 314 out.println("Error while generating PDF:"); 292 315 e.printStackTrace(out); 293 out.close();294 316 295 317 LOGGER.error("Error while generating PDF", e); 296 318 } catch (IOException ex) { 297 319 throw new RuntimeException(e); 320 } finally { 321 if (out != null) { 322 out.close(); 323 } 298 324 } 299 325 } … … 303 329 */ 304 330 protected void error(HttpServletResponse httpServletResponse, String message, int code) { 331 PrintWriter out = null; 305 332 try { 306 333 httpServletResponse.setContentType("text/plain"); 307 334 httpServletResponse.setStatus(code); 308 PrintWriterout = httpServletResponse.getWriter();335 out = httpServletResponse.getWriter(); 309 336 out.println("Error while generating PDF:"); 310 337 out.println(message); 311 out.close(); 338 312 339 LOGGER.error("Error while generating PDF: " + message); 313 340 } catch (IOException ex) { 314 341 throw new RuntimeException(ex); 342 } finally { 343 if (out != null) { 344 out.close(); 345 } 315 346 } 316 347 }
