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.ImageResizeConverterSpec;
36: import org.objectweb.jonas.emb.mfb.formats.image.ImageMediaFormat;
37:
38: import emb.sample.MediaSampleException;
39: import emb.sample.session.MediaSampleSessionLocal;
40:
41: /**
42: * @author Brice Ruzand
43: */
44: public class ConvertSampleImageHalfSize extends BaseSampleServlet {
45:
46: /**
47: * serialVersionUID
48: */
49: private static final long serialVersionUID = 36899150987141944L;
50:
51: /**
52: * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest,
53: * javax.servlet.http.HttpServletResponse)
54: * @inheritDoc
55: */
56: public void service(HttpServletRequest request,
57: HttpServletResponse response) throws ServletException {
58: try {
59: MediaSampleSessionLocal session = getSession(request);
60:
61: // Query the media entity EJB instance.
62: String identity = request.getParameter("identity");
63: MediaEntityLocal mediaEntity = session
64: .getMediaFromPrimaryKey(identity);
65:
66: if (!(mediaEntity.getFormat() instanceof ImageMediaFormat)) {
67: throw new MediaSampleException(
68: "The selected media must be an image.");
69: }
70:
71: // Create a copy
72: MediaEntityLocal media = session
73: .copyMediaEntity(mediaEntity);
74: media.setDescription("Half-size conversion of: "
75: + mediaEntity.getName());
76:
77: // Convert the MediaEntityToHalfsize
78: final double resizeFactor = 0.5;
79: MediaConverterSpec[] specs = new MediaConverterSpec[] { new ImageResizeConverterSpec(
80: resizeFactor) };
81: media.convert(specs);
82: identity = (String) media.getPrimaryKey();
83:
84: // forward onto the retrieve servlet
85: request.setAttribute("identity", media.getPrimaryKey());
86: this .getServletContext().getRequestDispatcher(
87: RETRIEVE_SERVLET).forward(request, response);
88:
89: } catch (Throwable e) {
90: exceptionHandler(e, getClass(), request, response);
91: }
92: }
93:
94: }
|