001: package org.enhydra.snapperPreviewer.presentation;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.InputStream;
006: import java.io.OutputStream;
007: import java.util.Properties;
008:
009: import org.enhydra.snapper.SnapperManager;
010: import org.enhydra.snapperPreviewer.Previewer;
011: import org.enhydra.snapperPreviewer.spec.Download;
012: import org.enhydra.snapperPreviewer.spec.DownloadFactory;
013: import org.enhydra.snapperPreviewer.spec.FileContainerManager;
014: import org.enhydra.snapperPreviewer.spec.FileContainerManagerFactory;
015: import org.enhydra.snapperPreviewer.spec.Path;
016: import org.enhydra.snapperPreviewer.spec.PictureManager;
017: import org.enhydra.snapperPreviewer.spec.PictureManagerFactory;
018: import org.enhydra.snapperPreviewer.spec.Site;
019: import org.enhydra.snapperPreviewer.spec.SiteList;
020: import org.enhydra.snapperPreviewer.spec.SiteListFactory;
021: import org.enhydra.xml.xmlc.XMLObject;
022:
023: /**
024: * @author Milin Radivoj
025: *
026: */
027: public class ShowPicture extends BasePO {
028:
029: Properties pictureConversionRequestParm = new Properties();
030:
031: protected XMLObject getDOM() throws Exception {
032:
033: boolean isContainer = false;
034: FileContainerManager manager = null;
035:
036: try {
037: String path = comms.request.getParameter("url");
038: String type = comms.request.getParameter("type");
039: String id = comms.request.getParameter("siteName");
040: managePictureRequestParameters();
041: if (path == null) {
042: Previewer.logInfo("Missing url parameter");
043: showErrorPicture();
044: return null;
045: }
046:
047: String fileToShow = null;
048:
049: if (Previewer.isRelitive() && id != null) {
050: try {
051: SiteList sl = SiteListFactory
052: .getSiteList("org.enhydra.snapperPreviewer.business.SiteListImpl");
053:
054: Site site = sl.findSiteByName(id);
055:
056: Path[] pathList = site.getPathList();
057:
058: if (pathList != null && pathList.length == 1) {
059: String root = pathList[0].getRoot();
060:
061: path = root + "/" + path;
062:
063: }
064:
065: } catch (Exception e) {
066: //Previewer.logError(e.getMessage());
067: }
068:
069: }
070:
071: if (path.indexOf("|") != -1) {
072: manager = FileContainerManagerFactory
073: .getFileContainerManager("org.enhydra.snapperPreviewer.business.FileContainerManagerImpl");
074: try {
075: manager.init();
076: File containerFile = manager.getContainerFile(path,
077: id);
078: fileToShow = containerFile.getAbsolutePath();
079: } catch (Exception e) {
080: fileToShow = null;
081: }
082: isContainer = true;
083: } else {
084: fileToShow = path;
085: }
086:
087: if (fileToShow != null) {
088: int indexOfExtension = fileToShow.lastIndexOf(".");
089:
090: String typeOfFile = type;
091:
092: if (indexOfExtension != -1) {
093: typeOfFile = fileToShow.substring(
094: indexOfExtension + 1, fileToShow.length());
095: } else {
096: Previewer
097: .logInfo(" Missing extension of file name : ");
098: showErrorPicture();
099: if (isContainer && manager != null)
100: manager.deleteAllTempFiles();
101: return null;
102: }
103:
104: if (typeOfFile.equalsIgnoreCase("tiff")
105: || typeOfFile.equalsIgnoreCase("tif")
106: || typeOfFile.equalsIgnoreCase("g3f")
107: || typeOfFile.equalsIgnoreCase("g3")) {
108:
109: int indexOfPictureInPath = path.lastIndexOf("|");
110: String pictureId = "";
111:
112: if (indexOfPictureInPath != -1
113: && !(path.endsWith("tiff")
114: || path.endsWith("tif")
115: || path.endsWith("g3f")
116: || path.endsWith("g3")
117: || path.endsWith("TIFF")
118: || path.endsWith("TIF")
119: || path.endsWith("G3F") || path
120: .endsWith("G3"))) {
121: pictureId = path
122: .substring(indexOfPictureInPath + 1,
123: path.length());
124: parseAndShowPictureInPicture(pictureId,
125: fileToShow, typeOfFile);
126: } else {
127: showRegularFile(fileToShow, typeOfFile);
128: }
129: } else {
130: if (!chackSupportedPictureFormat(typeOfFile)) {
131: Previewer
132: .logInfo("Not supported picture file format : "
133: + typeOfFile);
134: showErrorPicture();
135: if (isContainer && manager != null)
136: manager.deleteAllTempFiles();
137: return null;
138:
139: }
140:
141: if (!fileToShow.startsWith("ftp")) {
142: showRegularFile(fileToShow, typeOfFile);
143: } else {
144: File tempFile = downloadFtpFile(fileToShow,
145: typeOfFile);
146: if (tempFile != null) {
147: showRegularFile(tempFile.getAbsolutePath(),
148: typeOfFile);
149: tempFile.delete();
150: } else {
151: showErrorPicture();
152: if (isContainer && manager != null)
153: manager.deleteAllTempFiles();
154: return null;
155: }
156: }
157: }
158: } else {
159: showErrorPicture();
160: }
161: } catch (Exception e) {
162: Previewer.logError(e.getMessage());
163: }
164:
165: if (isContainer && manager != null)
166: manager.deleteAllTempFiles();
167:
168: return null;
169: }
170:
171: protected void showRegularFile(String filePath, String type)
172: throws Exception {
173: try {
174: File f = new File(filePath);
175: PictureManager resize = PictureManagerFactory
176: .getResize("org.enhydra.snapperPreviewer.business.PictureManagerImpl");
177:
178: OutputStream os = comms.response.getOutputStream();
179: int scale = Previewer.getPicturePixel();
180: int colorNum = Previewer.getPictureColorNumber();
181: String outputFileFormat = Previewer.getOutputFileFormat();
182: boolean useGrayFilter = Previewer.getUseGrayFilter();
183:
184: if (pictureConversionRequestParm
185: .getProperty("maxPixelNumber") != null) {
186: try {
187: scale = (new Integer(pictureConversionRequestParm
188: .getProperty("maxPixelNumber"))).intValue();
189: } catch (Exception e) {
190: scale = Previewer.getPicturePixel();
191: }
192: }
193:
194: if (pictureConversionRequestParm.getProperty("reduceColor") != null) {
195: try {
196:
197: colorNum = (new Integer(
198: pictureConversionRequestParm
199: .getProperty("reduceColor")))
200: .intValue();
201: } catch (Exception e) {
202: colorNum = Previewer.getPictureColorNumber();
203: }
204: }
205:
206: if (pictureConversionRequestParm
207: .getProperty("forceOutputFileFormat") != null) {
208: try {
209: outputFileFormat = pictureConversionRequestParm
210: .getProperty("forceOutputFileFormat");
211: } catch (Exception e) {
212: outputFileFormat = Previewer.getOutputFileFormat();
213: }
214: }
215:
216: if (pictureConversionRequestParm
217: .getProperty("useGrayFilter") != null) {
218: String temp = pictureConversionRequestParm
219: .getProperty("useGrayFilter");
220: if (temp.equals("true"))
221: useGrayFilter = true;
222: else if (temp.equals("false"))
223: useGrayFilter = false;
224: }
225:
226: String mapped = mapMimiType(type);
227:
228: if (!outputFileFormat.equals("no")) {
229: comms.response.setContentType(outputFileFormat);
230: } else {
231: comms.response.setContentType(mapped);
232: outputFileFormat = mapped;
233: }
234:
235: resize.writePicture(filePath, os, outputFileFormat, scale,
236: colorNum, useGrayFilter, Previewer
237: .getXslDirectory()
238: + "/" + "pictureError.jpg");
239: os.flush();
240: comms.response.flush();
241:
242: } catch (Exception e) {
243: writeErrorPicture();
244: }
245: }
246:
247: protected void parseAndShowPictureInPicture(String pictureId,
248: String fileToShow, String type) throws Exception {
249:
250: PictureManager pictManager = PictureManagerFactory
251: .getResize("org.enhydra.snapperPreviewer.business.PictureManagerImpl");
252:
253: try {
254: int scale = Previewer.getPicturePixel();
255:
256: if (pictureConversionRequestParm
257: .getProperty("maxPixelNumber") != null) {
258: try {
259: scale = (new Integer(pictureConversionRequestParm
260: .getProperty("maxPixelNumber"))).intValue();
261: } catch (Exception e) {
262: scale = Previewer.getPicturePixel();
263: }
264: }
265: OutputStream os = comms.response.getOutputStream();
266:
267: pictManager.writePictureFromPicture(fileToShow, pictureId,
268: os, scale, Previewer.getXslDirectory() + "/"
269: + "pictureError.jpg");
270: } catch (Exception e) {
271: writeErrorPicture();
272: }
273:
274: }
275:
276: private File downloadFtpFile(String id, String type) {
277: try {
278: Download dw = DownloadFactory
279: .getDownload("org.enhydra.snapper.business.DownloadImpl");
280: File retVal = dw.downloadFtpFile(SnapperManager
281: .getInstance().getTempDir(), id, type);
282: if (retVal != null) {
283: return retVal;
284: } else {
285: Previewer.logError(" Error geting ftp file ");
286: return null;
287: }
288: } catch (Exception e) {
289: writeErrorPicture();
290: }
291: return null;
292: }
293:
294: public String mapMimiType(String typeOfFile) {
295:
296: if ("xbm".equalsIgnoreCase(typeOfFile))
297: return "image/xbm";
298: else if ("ico".equalsIgnoreCase(typeOfFile))
299: return "image/jpeg";
300: else if ("gif".equalsIgnoreCase(typeOfFile))
301: return "image/jpeg";
302: else if ("jpg".equalsIgnoreCase(typeOfFile))
303: return "image/jpeg";
304: else if ("tiff".equalsIgnoreCase(typeOfFile))
305: return "image/png";
306: else if ("tif".equalsIgnoreCase(typeOfFile))
307: return "image/png";
308: else if ("jpeg".equalsIgnoreCase(typeOfFile))
309: return "image/jpeg";
310: else if ("pict".equalsIgnoreCase(typeOfFile))
311: return "image/pict";
312: else if ("bmp".equalsIgnoreCase(typeOfFile))
313: return "image/bmp";
314: else if ("psd".equalsIgnoreCase(typeOfFile))
315: return "image/psd";
316: else if ("pcx".equalsIgnoreCase(typeOfFile))
317: return "image/pcx";
318: else if ("cur".equalsIgnoreCase(typeOfFile))
319: return "image/jpeg";
320: else if ("xpm".equalsIgnoreCase(typeOfFile))
321: return "image/xpm";
322: else if ("png".equalsIgnoreCase(typeOfFile))
323: return "image/png";
324: else if ("targa".equalsIgnoreCase(typeOfFile))
325: return "image/jpeg";
326: else
327: return "image/jpeg";
328: }
329:
330: public boolean chackSupportedPictureFormat(String typeOfFile) {
331:
332: if ("xbm".equalsIgnoreCase(typeOfFile))
333: return true;
334: else if ("ico".equalsIgnoreCase(typeOfFile))
335: return true;
336: else if ("gif".equalsIgnoreCase(typeOfFile))
337: return true;
338: else if ("jpg".equalsIgnoreCase(typeOfFile))
339: return true;
340: else if ("tiff".equalsIgnoreCase(typeOfFile))
341: return true;
342: else if ("tif".equalsIgnoreCase(typeOfFile))
343: return true;
344: else if ("jpeg".equalsIgnoreCase(typeOfFile))
345: return true;
346: else if ("pict".equalsIgnoreCase(typeOfFile))
347: return true;
348: else if ("bmp".equalsIgnoreCase(typeOfFile))
349: return true;
350: else if ("psd".equalsIgnoreCase(typeOfFile))
351: return true;
352: else if ("pcx".equalsIgnoreCase(typeOfFile))
353: return true;
354: else if ("cur".equalsIgnoreCase(typeOfFile))
355: return true;
356: else if ("xpm".equalsIgnoreCase(typeOfFile))
357: return true;
358: else if ("png".equalsIgnoreCase(typeOfFile))
359: return true;
360: else if ("targa".equalsIgnoreCase(typeOfFile))
361: return true;
362: else if ("g3f".equalsIgnoreCase(typeOfFile))
363: return true;
364: else
365: return false;
366:
367: }
368:
369: private void showErrorPicture() {
370: try {
371: PictureManager resize = PictureManagerFactory
372: .getResize("org.enhydra.snapperPreviewer.business.PictureManagerImpl");
373: OutputStream os = comms.response.getOutputStream();
374: int scale = 400;
375: int colorNum = 0;
376: boolean useGrayFilter = false;
377: comms.response.setContentType("image/jpeg");
378: resize.writePicture(Previewer.getXslDirectory()
379: + File.separator + "pictureError.jpg", os,
380: "image/jpeg", scale, colorNum, useGrayFilter,
381: Previewer.getXslDirectory() + "/"
382: + "pictureError.jpg");
383: os.flush();
384: comms.response.flush();
385: } catch (Exception e) {
386: writeErrorPicture();
387: }
388: }
389:
390: private void writeErrorPicture() {
391: try {
392: OutputStream os = comms.response.getOutputStream();
393: comms.response.setContentType("image/jpeg");
394: File errorPicture = new File(Previewer.getXslDirectory()
395: + File.separator + "pictureError.jpg");
396:
397: InputStream is = null;
398:
399: try {
400: is = new FileInputStream(errorPicture);
401: byte buffer[] = new byte[1024];
402: int read = -1;
403: while ((read = is.read(buffer, 0, 1024)) != -1) {
404: os.write(buffer, 0, read);
405: }
406: os.flush();
407: } catch (Exception e) {
408: Previewer
409: .logError(" Error in writing error picture to output stream : "
410: + e.getMessage());
411: return;
412: } finally {
413: if (is != null) {
414: try {
415: is.close();
416: } catch (Exception e) {
417: }
418:
419: is = null;
420: }
421: }
422:
423: comms.response.flush();
424: } catch (Exception e) {
425: Previewer.logError(e.getMessage());
426: }
427: }
428:
429: private void managePictureRequestParameters() throws Exception {
430: String pixelNum = comms.request.getParameter("maxPixelNumber");
431: String reduceColor = comms.request.getParameter("reduceColor");
432: String forceOut = comms.request
433: .getParameter("forceOutputFileFormat");
434: String useGrayFilter = comms.request
435: .getParameter("useGrayFilter");
436: String zoomed = comms.request.getParameter("zoomed");
437:
438: if (pixelNum != null && !pixelNum.equals(""))
439: pictureConversionRequestParm.setProperty("maxPixelNumber",
440: pixelNum);
441:
442: if (reduceColor != null && !reduceColor.equals(""))
443: pictureConversionRequestParm.setProperty("reduceColor",
444: reduceColor);
445:
446: if (forceOut != null && !forceOut.equals(""))
447: pictureConversionRequestParm.setProperty(
448: "forceOutputFileFormat", forceOut);
449:
450: if (useGrayFilter != null && !useGrayFilter.equals(""))
451: pictureConversionRequestParm.setProperty("useGrayFilter",
452: useGrayFilter);
453:
454: if (zoomed != null && !zoomed.equals(""))
455: pictureConversionRequestParm.setProperty("zoomed", zoomed);
456: }
457:
458: }
|