01: /* *****************************************************************************
02: * MediaConverter.java
03: * ****************************************************************************/
04:
05: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
06: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
07: * Use is subject to license terms. *
08: * J_LZ_COPYRIGHT_END *********************************************************/
09:
10: package org.openlaszlo.data;
11:
12: import java.io.*;
13: import java.net.MalformedURLException;
14:
15: import javax.servlet.http.HttpServletRequest;
16: import javax.servlet.http.HttpServletResponse;
17:
18: import org.openlaszlo.media.MimeType;
19: import org.openlaszlo.media.Transcoder;
20: import org.openlaszlo.media.TranscoderException;
21: import org.openlaszlo.utils.ChainedException;
22:
23: import org.apache.log4j.*;
24:
25: /**
26: * Media Converter
27: */
28: public class MediaConverter extends Converter {
29:
30: private static Logger mLogger = Logger
31: .getLogger(MediaConverter.class);
32:
33: /**
34: * Convert incoming Media to SWF
35: */
36: public InputStream convertToSWF(Data data, HttpServletRequest req,
37: HttpServletResponse res) throws ConversionException,
38: IOException {
39:
40: String surl = null;
41: try {
42: surl = DataSource.getURL(req);
43: } catch (MalformedURLException e) {
44: throw new ChainedException(e);
45: }
46: int index = surl.indexOf('?');
47: String path = surl;
48: if (index > 0) {
49: path = surl.substring(0, index);
50: }
51:
52: String mimeType = data.getMimeType();
53: if (!Transcoder.canTranscode(mimeType, MimeType.SWF)) {
54: // If we can't convert the content type, try from the extension
55: mimeType = MimeType.fromExtension(path);
56: }
57:
58: mLogger.debug(
59: /* (non-Javadoc)
60: * @i18n.test
61: * @org-mes="calling transcoder on " + "mime type: " + p[0]
62: */
63: org.openlaszlo.i18n.LaszloMessages.getMessage(
64: MediaConverter.class.getName(), "051018-62",
65: new Object[] { mimeType }));
66:
67: try {
68: return Transcoder.transcode(data.getInputStream(),
69: mimeType, MimeType.SWF, /* do stream audio */true);
70: } catch (TranscoderException e) {
71: throw new ConversionException(e.getMessage());
72: }
73: }
74:
75: /**
76: * media should be as is
77: */
78: public String chooseEncoding(HttpServletRequest req) {
79: return null;
80: }
81:
82: }
|