1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.capabilities.impl;
1819import org.apache.commons.logging.Log;
20import org.apache.commons.logging.LogFactory;
21import org.apache.jetspeed.capabilities.CapabilityMap;
22import org.apache.jetspeed.capabilities.Client;
23import org.apache.jetspeed.capabilities.Capability;
24import org.apache.jetspeed.capabilities.MediaType;
2526import java.util.HashMap;
27import java.util.Iterator;
28import java.util.Map;
2930import org.apache.jetspeed.capabilities.MimeType;
3132/***33 * Implementation for capabilityMap interface34 *35 * @author <a href="mailto:roger.ruttimann@earthlink.net">Roger Ruttimann</a>36 * @version $Id: CapabilityMapImpl.java 553014 2007-07-03 23:10:53Z ate $37 */38classCapabilityMapImpl implements CapabilityMap
39 {
40privatestaticfinal Log log =
41 LogFactory.getLog(JetspeedCapabilities.class);
4243// Members44private String useragent; // User agent for request45private Map mimeTypeMap = new HashMap(); // supported Mimetypes for Agent46private Map capabilityMap = new HashMap();
47// supported Capabilities for Agent48private Map mediaTypeMap = new HashMap(); // supported MediaTypes for Agent49private Client client; // client for Agent50private MediaType preferredMediaType; // Preferred MediaType for client.5152/***53 Sets the client for the CapabilityMap54 */55publicvoid setClient(Client client)
56 {
57this.client = client;
58 }
5960/***61 Returns the Client for the CapabilityMap62 */63public Client getClient()
64 {
65returnthis.client;
66 }
6768/***69 Add capability to the CapabilityMap70 */71publicvoid addCapability(Capability capability)
72 {
73if (capability != null) // avoid null due to duplicates in database 74this.capabilityMap.put(capability.getName(), capability);
75 }
7677/***78 Add Mimetype to the MimetypeMap79 */80publicvoid addMimetype(MimeType mimetype)
81 {
82if (mimetype != null) // avoid null due to duplicates in database83this.mimeTypeMap.put(mimetype.getName(), mimetype);
84 }
8586/***87 Add MediaType to the MediaTypeMap88 */89publicvoid addMediaType(MediaType mediatype)
90 {
91if (mediatype != null) // avoid null due to duplicates in database92this.mediaTypeMap.put(mediatype.getName(), mediatype);
93 }
9495/***96 Returns the preferred MIME type for the current user-agent97 */98public MimeType getPreferredType()
99 {
100// Return the value that matches the preferredMimeType defined in the Client101int prefMimeTypeId = this.client.getPreferredMimeTypeId();
102103 MimeType mt = null;
104 Iterator e = this.mimeTypeMap.values().iterator();
105while (e.hasNext())
106 {
107 mt = (MimeType) e.next();
108109if (mt.getMimetypeId() == prefMimeTypeId)
110return mt;
111 }
112 log.error("Could not find preferred Mime Type for " + prefMimeTypeId);
113114// Should never reach this point. A preferred value needs to be set115returnnull; // TODO: NEVER RETURN NULL116 }
117118/***119 * Sets the preferred MediaType for this CapabilityMap120 * @param MediaTypeEntry 121 */122publicvoid setPreferredMediaType(MediaType type)
123 {
124this.preferredMediaType = type;
125 }
126127/***128 Returns the preferred media type for the current user-agent129 */130public MediaType getPreferredMediaType()
131 {
132returnthis.preferredMediaType;
133 }
134135/***136 * Returns an ordered list of supported media-types, from most preferred137 * to least preferred138 */139public Iterator listMediaTypes()
140 {
141return mediaTypeMap.values().iterator();
142 }
143144/***145 Returns the user-agent string146 */147public String getAgent()
148 {
149returnthis.useragent;
150 }
151152/***153 * set userAgent154 */155publicvoid setAgent(String userAgent)
156 {
157this.useragent = userAgent;
158 }
159160/***161 * Checks to see if the current agent has the specified capability162 */163publicboolean hasCapability(int capability)
164 {
165 Iterator capabilities = capabilityMap.values().iterator();
166while (capabilities.hasNext())
167 {
168if (((Capability) capabilities.next()).getCapabilityId()
169 == capability)
170 {
171returntrue;
172 }
173 }
174return false;
175 }
176177/***178 * Checks to see if the current agent has the specified capability179 */180publicboolean hasCapability(String capability)
181 {
182 Iterator capabilities = capabilityMap.values().iterator();
183while (capabilities.hasNext())
184 {
185if (((Capability) capabilities.next()).getName().equals(capability))
186 {
187returntrue;
188 }
189 }
190return false;
191 }
192193/***194 Get the mime types that this CapabilityMap supports.195 */196public Iterator getMimeTypes()
197 {
198return mimeTypeMap.values().iterator();
199 }
200201/***202 Return true if this CapabilityMap supports the given MimeType203 */204publicboolean supportsMimeType(MimeType mimeType)
205 {
206 Iterator mimetypes = mimeTypeMap.values().iterator();
207while (mimetypes.hasNext())
208 {
209if (((MimeType) mimetypes.next()).getName().equals(mimeType.getName()))
210 {
211returntrue;
212 }
213 }
214return false;
215 }
216217/***218 * Return true if this CapabilityMap supports the given media type219 *220 * @param media the name of a media type registered in the221 * MediaType registry222 *223 * @return true is the capabilities of this agent at least match those224 * required by the media type225 */226publicboolean supportsMediaType(String media)
227 {
228 Iterator mediatypes = mediaTypeMap.values().iterator();
229while (mediatypes.hasNext())
230 {
231if (((MediaType) mediatypes.next()).getName() == media)
232 {
233returntrue;
234 }
235 }
236return false;
237 }
238239/***240 * Create a map -> string representation241 */242public String toString()
243 {
244return"";
245 }
246247 }