KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > components > DefaultMediaManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.webapps.session.components;
17
18 import org.apache.avalon.framework.component.Component;
19 import org.apache.avalon.framework.configuration.Configurable;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.context.Context;
23 import org.apache.avalon.framework.context.ContextException;
24 import org.apache.avalon.framework.context.Contextualizable;
25 import org.apache.avalon.framework.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27 import org.apache.cocoon.components.ContextHelper;
28 import org.apache.cocoon.environment.Request;
29 import org.apache.cocoon.webapps.session.MediaManager;
30
31 /**
32  * This is the default implementation for the media manager
33  *
34  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
35  * @version CVS $Id: DefaultMediaManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
36 */

37 public final class DefaultMediaManager
38 extends AbstractLogEnabled
39 implements MediaManager, Configurable, ThreadSafe, Contextualizable, Component {
40
41     /** The media Types */
42     protected PreparedMediaType[] allMediaTypes;
43     
44     /** The default media type (usually this is html) */
45     protected String JavaDoc defaultMediaType;
46     
47     /** All media type names */
48     protected String JavaDoc[] mediaTypeNames;
49
50     /** The Context */
51     protected Context context;
52     
53     
54     /* (non-Javadoc)
55      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
56      */

57     public void contextualize(Context context) throws ContextException {
58         this.context = context;
59     }
60
61     /* (non-Javadoc)
62      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
63      */

64     public void configure(Configuration myConfiguration)
65     throws ConfigurationException {
66         // no sync required
67
Configuration mediaConf = myConfiguration.getChild("mediatypes", false);
68         if (mediaConf == null) {
69             // default configuration
70
this.defaultMediaType = "html";
71         } else {
72             this.defaultMediaType = mediaConf.getAttribute("default", "html");
73         }
74         this.mediaTypeNames = new String JavaDoc[1];
75         this.mediaTypeNames[0] = this.defaultMediaType;
76         boolean found;
77         int i;
78         String JavaDoc name;
79
80         Configuration[] childs = mediaConf.getChildren("media");
81         PreparedMediaType[] array = new PreparedMediaType[0];
82         PreparedMediaType[] copy;
83         Configuration current;
84         if (childs != null) {
85             for(int x = 0; x < childs.length; x++) {
86                 current = childs[x];
87                 copy = new PreparedMediaType[array.length + 1];
88                 System.arraycopy(array, 0, copy, 0, array.length);
89                 array = copy;
90                 name = current.getAttribute("name");
91                 array[array.length-1] = new PreparedMediaType(name, current.getAttribute("useragent"));
92                 found = false;
93                 i = 0;
94                 while ( i < this.mediaTypeNames.length && found == false) {
95                     found = this.mediaTypeNames[i].equals(name);
96                     i++;
97                 }
98                 if (found == false) {
99                     String JavaDoc[] newStrings = new String JavaDoc[this.mediaTypeNames.length + 1];
100                     System.arraycopy(this.mediaTypeNames, 0, newStrings, 0, this.mediaTypeNames.length);
101                     newStrings[newStrings.length-1] = name;
102                     this.mediaTypeNames = newStrings;
103                 }
104             }
105         }
106         this.allMediaTypes = array;
107     }
108
109     /**
110      * Test if the media of the current request is the given value
111      */

112     public boolean testMedia(String JavaDoc value) {
113         // synchronized
114
boolean result = false;
115
116         Request request = ContextHelper.getRequest(this.context);
117         
118         String JavaDoc useragent = request.getHeader("User-Agent");
119         PreparedMediaType theMedia = null;
120         int i, l;
121         i = 0;
122         l = this.allMediaTypes.length;
123         while (i < l && theMedia == null) {
124             if (useragent.indexOf(this.allMediaTypes[i].useragent) == -1) {
125                 i++;
126             } else {
127                 theMedia = this.allMediaTypes[i];
128             }
129         }
130         if (theMedia != null) {
131             result = theMedia.name.equals(value);
132         } else {
133             result = this.defaultMediaType.equals(value);
134         }
135
136         return result;
137     }
138
139     /* (non-Javadoc)
140      * @see org.apache.cocoon.webapps.session.MediaManager#getMediaTypes()
141      */

142     public String JavaDoc[] getMediaTypes() {
143         // synchronized
144
return this.mediaTypeNames;
145     }
146
147     /* (non-Javadoc)
148      * @see org.apache.cocoon.webapps.session.MediaManager#getMediaType()
149      */

150     public String JavaDoc getMediaType() {
151         // synchronized
152
Request request = ContextHelper.getRequest( this.context );
153         // get the media of the current request
154
String JavaDoc useragent = request.getHeader("User-Agent");
155         PreparedMediaType media = null;
156         if (useragent != null) {
157             int i, l;
158             i = 0;
159             l = this.allMediaTypes.length;
160             while (i < l && media == null) {
161                 if (useragent.indexOf(this.allMediaTypes[i].useragent) == -1) {
162                     i++;
163                 } else {
164                     media = this.allMediaTypes[i];
165                 }
166             }
167         }
168         return (media == null ? this.defaultMediaType : media.name);
169     }
170
171 }
172
173
174 /**
175  * This class stores the media type configuration
176  */

177 final class PreparedMediaType {
178
179     String JavaDoc name;
180     String JavaDoc useragent;
181
182     PreparedMediaType(String JavaDoc name, String JavaDoc useragent) {
183         this.name = name;
184         this.useragent = useragent;
185     }
186 }
187
Popular Tags