01: /*
02: * JOnAS: Java(TM) Open Application Server
03: * Copyright (C) 1999-2005 Bull S.A.
04: * Contact: jonas-team@objectweb.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19: * USA
20: *
21: * Initial developer(s): Brice Ruzand
22: *
23: * --------------------------------------------------------------------------
24: * $Id$
25: * --------------------------------------------------------------------------
26: */
27: package emb.sample.servlet;
28:
29: import javax.emb.MediaConverterSpec;
30: import javax.emb.MediaEntityLocal;
31: import javax.servlet.ServletException;
32: import javax.servlet.http.HttpServletRequest;
33: import javax.servlet.http.HttpServletResponse;
34:
35: import org.objectweb.jonas.emb.mfb.converter.image.ImageFormatConverterSpec;
36: import org.objectweb.jonas.emb.mfb.converter.image.ImageRGBtoGrayscaleConverterSpec;
37: import org.objectweb.jonas.emb.mfb.converter.image.ImageResizeConverterSpec;
38: import org.objectweb.jonas.emb.mfb.formats.image.ImageMediaFormat;
39:
40: import emb.sample.MediaSampleException;
41: import emb.sample.session.MediaSampleSessionLocal;
42:
43: /**
44: * @author Brice Ruzand
45: */
46: public class ConvertSampleImageChained extends BaseSampleServlet {
47:
48: /**
49: * serialVersionUID
50: */
51: private static final long serialVersionUID = 3761970445343076657L;
52:
53: /**
54: * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest,
55: * javax.servlet.http.HttpServletResponse)
56: * @inheritDoc
57: */
58: public void service(HttpServletRequest request,
59: HttpServletResponse response) throws ServletException {
60: try {
61: MediaSampleSessionLocal session = getSession(request);
62:
63: // Query the media entity EJB instance.
64: String identity = request.getParameter("identity");
65: MediaEntityLocal mediaEntity = session
66: .getMediaFromPrimaryKey(identity);
67:
68: if (!(mediaEntity.getFormat() instanceof ImageMediaFormat)) {
69: throw new MediaSampleException(
70: "The selected media must be an image.");
71: }
72:
73: // Create a copy
74: MediaEntityLocal media = session
75: .copyMediaEntity(mediaEntity);
76: media.setDescription("Chained convertion of: "
77: + mediaEntity.getName());
78:
79: // Convert the media to PNG then to Grayscale then to Halfsize
80: final double resizeFactor = 0.5;
81: MediaConverterSpec[] specs = new MediaConverterSpec[] {
82: new ImageFormatConverterSpec("png"),
83: new ImageRGBtoGrayscaleConverterSpec(),
84: new ImageResizeConverterSpec(resizeFactor) };
85: media.convert(specs);
86: identity = (String) media.getPrimaryKey();
87:
88: // forward onto the retrieve servlet
89: request.setAttribute("identity", media.getPrimaryKey());
90: this .getServletContext().getRequestDispatcher(
91: RETRIEVE_SERVLET).forward(request, response);
92:
93: } catch (Throwable e) {
94: exceptionHandler(e, getClass(), request, response);
95: }
96: }
97:
98: }
|