001: package com.sun.portal.wireless.transcode;
002:
003: import java.util.Set;
004: import java.util.Iterator;
005: import java.io.OutputStream;
006: import java.io.InputStream;
007: import java.io.IOException;
008: import java.util.StringTokenizer;
009: import java.util.HashMap;
010: import java.util.Vector;
011: import java.net.URL;
012: import java.io.ByteArrayOutputStream;
013:
014: import javax.servlet.*;
015: import javax.servlet.http.*;
016:
017: import com.iplanet.am.util.AMClientDetector;
018: import com.iplanet.services.cdm.Client;
019: import com.iplanet.services.cdm.ClientsManager;
020: import com.iplanet.services.cdm.ClientException;
021:
022: public class TranscodeServlet extends HttpServlet {
023:
024: AMClientDetector cd;
025:
026: public void init() {
027: cd = new AMClientDetector();
028: }
029:
030: // Request parameters:
031: // img: the URI of the image
032: // targetType: optional; the type to which it should be encoded. If not provided, the servlet defaults to:
033: // 1. The original type, if it is found in the accept types
034: // 2. Any of the types found in accept types that has a transcoder available
035: // scalex, scaley: optional; scale factor in percentage of display size. If not provided, defaults to no scaling
036:
037: public void doGet(HttpServletRequest request,
038: HttpServletResponse response) {
039:
040: String clientType = cd.getClientType(request);
041: Client client = null;
042: int screenx = 0, screeny = 0;
043: Set accept;
044: String img, targetType;
045: int scalex = 0, scaley = 0;
046: boolean transcode = true;
047: try {
048: client = ClientsManager.getInstance(clientType);
049: } catch (ClientException ce) {
050: try {
051: response.sendError(response.SC_INTERNAL_SERVER_ERROR);
052: } catch (IOException e) {
053: }
054: }
055: try {
056: Transcoder trans = new Transcoder();
057:
058: accept = client
059: .getProperties(TranscodeConstants.ACCEPT_PROPERTY);
060:
061: String t;
062: img = request.getParameter(TranscodeConstants.IMG);
063: targetType = request
064: .getParameter(TranscodeConstants.TARGET_TYPE);
065: t = request.getParameter(TranscodeConstants.SCALE_X);
066: if (t != null) {
067: scalex = Integer.parseInt(t);
068: }
069: t = request.getParameter(TranscodeConstants.SCALE_Y);
070: if (t != null) {
071: scaley = Integer.parseInt(t);
072: }
073:
074: if (!(img.startsWith("/") || img.startsWith("http://"))) {
075: // System.out.println("Image URIs must begin with '/' or 'http://'");
076: response.sendError(response.SC_INTERNAL_SERVER_ERROR);
077: return;
078: }
079: URL imgurl;
080: if (img.startsWith("/")) {
081: imgurl = getServletContext().getResource(img);
082: } else {
083: imgurl = new URL(img);
084: }
085: if (imgurl == null) {
086: response.sendError(response.SC_NOT_FOUND);
087: return;
088:
089: }
090: if (trans.setImage(imgurl) == null) {
091: // System.out.println("Could not determine type of image");
092: response.sendError(response.SC_INTERNAL_SERVER_ERROR);
093: return;
094: }
095: String format = trans.setTargetType(targetType, accept);
096: if (format == null) {
097: // System.out.println("Could not determine target type");
098: response.sendError(response.SC_INTERNAL_SERVER_ERROR);
099: return;
100: }
101:
102: if (scalex != 0 && scaley != 0) {
103: trans.setX(scalex);
104: trans.setY(scaley);
105: }
106:
107: ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
108: String ctype = trans.transcode(byteArray);
109:
110: response.setContentType(ctype);
111: OutputStream os = response.getOutputStream();
112: byteArray.writeTo(os);
113:
114: } catch (IOException e) {
115: System.out.println("Exception caught: " + e.getMessage());
116: e.printStackTrace();
117: try {
118: response.sendError(response.SC_INTERNAL_SERVER_ERROR);
119: } catch (IOException err) {
120: }
121: }
122: }
123:
124: }
|