001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.webapps.session.components;
018:
019: import org.apache.avalon.framework.component.Component;
020: import org.apache.avalon.framework.configuration.Configurable;
021: import org.apache.avalon.framework.configuration.Configuration;
022: import org.apache.avalon.framework.configuration.ConfigurationException;
023: import org.apache.avalon.framework.context.Context;
024: import org.apache.avalon.framework.context.ContextException;
025: import org.apache.avalon.framework.context.Contextualizable;
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028: import org.apache.cocoon.components.ContextHelper;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.cocoon.webapps.session.MediaManager;
031:
032: /**
033: * This is the default implementation for the media manager
034: *
035: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
036: * @deprecated This block is deprecated and will be removed in future versions.
037: * @version CVS $Id: DefaultMediaManager.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public final class DefaultMediaManager extends AbstractLogEnabled
040: implements MediaManager, Configurable, ThreadSafe,
041: Contextualizable, Component {
042:
043: /** The media Types */
044: protected PreparedMediaType[] allMediaTypes;
045:
046: /** The default media type (usually this is html) */
047: protected String defaultMediaType;
048:
049: /** All media type names */
050: protected String[] mediaTypeNames;
051:
052: /** The Context */
053: protected Context context;
054:
055: /* (non-Javadoc)
056: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
057: */
058: public void contextualize(Context context) throws ContextException {
059: this .context = context;
060: }
061:
062: /* (non-Javadoc)
063: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
064: */
065: public void configure(Configuration myConfiguration)
066: throws ConfigurationException {
067: // no sync required
068: Configuration mediaConf = myConfiguration.getChild(
069: "mediatypes", false);
070: if (mediaConf == null) {
071: // default configuration
072: this .defaultMediaType = "html";
073: } else {
074: this .defaultMediaType = mediaConf.getAttribute("default",
075: "html");
076: }
077: this .mediaTypeNames = new String[1];
078: this .mediaTypeNames[0] = this .defaultMediaType;
079: boolean found;
080: int i;
081: String name;
082:
083: Configuration[] childs = mediaConf.getChildren("media");
084: PreparedMediaType[] array = new PreparedMediaType[0];
085: PreparedMediaType[] copy;
086: Configuration current;
087: if (childs != null) {
088: for (int x = 0; x < childs.length; x++) {
089: current = childs[x];
090: copy = new PreparedMediaType[array.length + 1];
091: System.arraycopy(array, 0, copy, 0, array.length);
092: array = copy;
093: name = current.getAttribute("name");
094: array[array.length - 1] = new PreparedMediaType(name,
095: current.getAttribute("useragent"));
096: found = false;
097: i = 0;
098: while (i < this .mediaTypeNames.length && found == false) {
099: found = this .mediaTypeNames[i].equals(name);
100: i++;
101: }
102: if (found == false) {
103: String[] newStrings = new String[this .mediaTypeNames.length + 1];
104: System.arraycopy(this .mediaTypeNames, 0,
105: newStrings, 0, this .mediaTypeNames.length);
106: newStrings[newStrings.length - 1] = name;
107: this .mediaTypeNames = newStrings;
108: }
109: }
110: }
111: this .allMediaTypes = array;
112: }
113:
114: /**
115: * Test if the media of the current request is the given value
116: */
117: public boolean testMedia(String value) {
118: // synchronized
119: boolean result = false;
120:
121: Request request = ContextHelper.getRequest(this .context);
122:
123: String useragent = request.getHeader("User-Agent");
124: PreparedMediaType theMedia = null;
125: int i, l;
126: i = 0;
127: l = this .allMediaTypes.length;
128: while (i < l && theMedia == null) {
129: if (useragent.indexOf(this .allMediaTypes[i].useragent) == -1) {
130: i++;
131: } else {
132: theMedia = this .allMediaTypes[i];
133: }
134: }
135: if (theMedia != null) {
136: result = theMedia.name.equals(value);
137: } else {
138: result = this .defaultMediaType.equals(value);
139: }
140:
141: return result;
142: }
143:
144: /* (non-Javadoc)
145: * @see org.apache.cocoon.webapps.session.MediaManager#getMediaTypes()
146: */
147: public String[] getMediaTypes() {
148: // synchronized
149: return this .mediaTypeNames;
150: }
151:
152: /* (non-Javadoc)
153: * @see org.apache.cocoon.webapps.session.MediaManager#getMediaType()
154: */
155: public String getMediaType() {
156: // synchronized
157: Request request = ContextHelper.getRequest(this .context);
158: // get the media of the current request
159: String useragent = request.getHeader("User-Agent");
160: PreparedMediaType media = null;
161: if (useragent != null) {
162: int i, l;
163: i = 0;
164: l = this .allMediaTypes.length;
165: while (i < l && media == null) {
166: if (useragent.indexOf(this .allMediaTypes[i].useragent) == -1) {
167: i++;
168: } else {
169: media = this .allMediaTypes[i];
170: }
171: }
172: }
173: return (media == null ? this .defaultMediaType : media.name);
174: }
175:
176: }
177:
178: /**
179: * This class stores the media type configuration
180: */
181: final class PreparedMediaType {
182:
183: String name;
184: String useragent;
185:
186: PreparedMediaType(String name, String useragent) {
187: this.name = name;
188: this.useragent = useragent;
189: }
190: }
|