KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > defaults > master > CmsShowMedia


1 package com.opencms.defaults.master;
2
3 /**
4 * This library is part of OpenCms -
5 * the Open Source Content Mananagement System
6 *
7 * Copyright (C) 2001 The OpenCms Group
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * For further information about OpenCms, please see the
20 * OpenCms Website: http://www.opencms.org
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */

26
27 import org.opencms.file.CmsObject;
28 import org.opencms.file.CmsRequestContext;
29 import org.opencms.main.CmsException;
30 import org.opencms.main.CmsLog;
31 import org.opencms.util.CmsUUID;
32
33 import com.opencms.legacy.CmsXmlTemplateLoader;
34 import com.opencms.template.CmsCacheDirectives;
35 import com.opencms.template.CmsXmlTemplate;
36
37 import java.lang.reflect.Constructor JavaDoc;
38 import java.lang.reflect.InvocationTargetException JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Vector JavaDoc;
41
42 /**
43  * XmlTemplate class to show media objects
44  *
45  * @deprecated Will not be supported past the OpenCms 6 release.
46  */

47 public class CmsShowMedia extends CmsXmlTemplate {
48
49     static final String JavaDoc C_EMPTY_PICTURE = "empty.gif";
50     static byte[] emptyGIF = new byte[0];
51
52     /**
53      * Gets the content of a defined section in a given template file and its
54      * subtemplates with the given parameters.
55      *
56      * @see #getContent(CmsObject, String, String, Hashtable, String)
57      *
58      * @param cms A_CmsObject Object for accessing system resources.
59      * @param templateFile Filename of the template file.
60      * @param elementName Element name of this template in our parent template.
61      * @param parameters Hashtable with all template class parameters.
62      * @param templateSelector template section that should be processed.
63      *
64      * @return It returns an array of bytes that contains the page.
65      */

66     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
67         Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
68         // session will be created or fetched
69
CmsRequestContext req = cms.getRequestContext();
70         byte[] picture = new byte[0];
71         CmsMasterContent cd = null;
72         CmsMasterMedia media = null;
73         String JavaDoc mType = null;
74         String JavaDoc sId = (String JavaDoc) parameters.get("id");
75         String JavaDoc sPos = (String JavaDoc) parameters.get("pos");
76         String JavaDoc refCD = (String JavaDoc) parameters.get("cd");
77         // media id to fetch exactly this media object
78
String JavaDoc smId = (String JavaDoc) parameters.get("mid");
79         CmsUUID id = CmsUUID.getNullUUID();
80         int pos =-1; // get first media per default ...
81
int mid = -1;
82         try {
83             id = new CmsUUID(sId);
84             if (sPos != null) {
85                 pos = Integer.parseInt(sPos);
86             }
87             if (smId != null) {
88                 mid = Integer.parseInt(smId);
89             }
90         } catch (NumberFormatException JavaDoc e) {
91             // ?
92
}
93
94         try {
95             Class JavaDoc c = Class.forName(refCD);
96             Object JavaDoc o = getContentDefinition(cms, c, id);
97             cd = (CmsMasterContent)o;
98         } catch (ClassNotFoundException JavaDoc e) {
99
100         }
101
102         // enable caching for this variant ...
103
Vector JavaDoc cosDeps = new Vector JavaDoc();
104         cosDeps.add(cd);
105         registerVariantDeps(cms, templateFile, null, null, parameters, null,
106                             cosDeps, null);
107
108         // read the media object ...
109
if(cd != null){
110             Vector JavaDoc vec = cd.getMedia();
111             if (mid != -1) {
112                 // walk through vector until media object with the mid is found
113
for (int i=0; i < vec.size(); i++) {
114                     media = (CmsMasterMedia)vec.get(i);
115                     if (mid == media.getId()) {
116                         picture = media.getMedia();
117                         mType = media.getMimetype();
118                         break;
119                     }
120                 }
121             }else if (pos == -1 && vec.size() > 0) {
122                 // got no pos info ..
123
media = (CmsMasterMedia)vec.firstElement();
124                 picture = media.getMedia();
125                 mType = media.getMimetype();
126             } else {
127                 // got pos info ...
128
for (int i=0; i< vec.size(); i++) {
129                     if (((CmsMasterMedia)vec.elementAt(i)).getPosition() == pos) {
130                         media = (CmsMasterMedia)vec.elementAt(i);
131                         picture = media.getMedia();
132                         mType = media.getMimetype();
133                         break;
134                     }
135                 }
136             }
137             if(picture == null){
138                 picture = emptyGIF;
139                 // set the mimetype ...
140
CmsXmlTemplateLoader.getResponse(req).setContentType("images/gif");
141             } else {
142                 // set mime type and filename in header
143
if (mType == null || mType.equals("")) {
144                     mType = "application/octet-stream";
145                 }
146                 CmsXmlTemplateLoader.getResponse(req).setContentType( mType );
147                 CmsXmlTemplateLoader.getResponse(req).setHeader("Content-disposition","filename=" + media.getName());
148             }
149         } else{
150             picture = emptyGIF;
151             // set the mimetype ...
152
CmsXmlTemplateLoader.getResponse(req).setContentType("images/gif");
153         }
154
155         /*if(req.isStreaming()) {
156             try {
157                 OutputStream os = req.getResponse().getOutputStream();
158                 os.write(picture);
159             } catch(Exception e) {
160                 throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, e);
161             }
162         }*/

163
164         return picture;
165     }
166
167
168      /**
169       * Gets the content definition class method constructor
170       * @return content definition object
171       */

172     protected Object JavaDoc getContentDefinition(CmsObject cms, Class JavaDoc cdClass, CmsUUID id) {
173         Object JavaDoc o = null;
174         try {
175             Constructor JavaDoc c = cdClass.getConstructor(new Class JavaDoc[] { CmsObject.class, CmsUUID.class });
176             o = c.newInstance(new Object JavaDoc[] { cms, id });
177         } catch (InvocationTargetException JavaDoc ite) {
178             if (CmsLog.getLog(this).isWarnEnabled()) {
179                 CmsLog.getLog(this).warn("Invocation target exception", ite);
180             }
181         } catch (NoSuchMethodException JavaDoc nsm) {
182             if (CmsLog.getLog(this).isWarnEnabled()) {
183                 CmsLog.getLog(this).warn("Requested method was not found", nsm);
184             }
185         } catch (InstantiationException JavaDoc e) {
186             if (CmsLog.getLog(this).isWarnEnabled()) {
187                 CmsLog.getLog(this).warn("The reflected class is abstract", e);
188             }
189         } catch (Exception JavaDoc e) {
190             if (CmsLog.getLog(this).isWarnEnabled()) {
191                 CmsLog.getLog(this).warn("Other exception", e);
192             }
193     
194         }
195         return o;
196     }
197
198    /**
199      * gets the caching information from the current template class.
200      *
201      * @param cms CmsObject Object for accessing system resources
202      * @param templateFile Filename of the template file
203      * @param elementName Element name of this template in our parent template.
204      * @param parameters Hashtable with all template class parameters.
205      * @param templateSelector template section that should be processed.
206      * @return <EM>true</EM> if this class may stream it's results, <EM>false</EM> otherwise.
207      */

208     public CmsCacheDirectives getCacheDirectives(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
209
210         CmsCacheDirectives ret = new CmsCacheDirectives(true, false, false, true, true);
211         Vector JavaDoc params = new Vector JavaDoc();
212         params.addElement("mid");
213         params.addElement("id");
214         params.addElement("pos");
215         params.addElement("cd");
216         ret.setNoCacheParameters(params);
217         return ret;
218     }
219
220
221
222 }
Popular Tags