1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.capabilities.impl;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.jetspeed.capabilities.CapabilityMap;
22 import org.apache.jetspeed.capabilities.Client;
23 import org.apache.jetspeed.capabilities.Capability;
24 import org.apache.jetspeed.capabilities.MediaType;
25
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29
30 import org.apache.jetspeed.capabilities.MimeType;
31
32 /***
33 * Implementation for capabilityMap interface
34 *
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 */
38 class CapabilityMapImpl implements CapabilityMap
39 {
40 private static final Log log =
41 LogFactory.getLog(JetspeedCapabilities.class);
42
43
44 private String useragent;
45 private Map mimeTypeMap = new HashMap();
46 private Map capabilityMap = new HashMap();
47
48 private Map mediaTypeMap = new HashMap();
49 private Client client;
50 private MediaType preferredMediaType;
51
52 /***
53 Sets the client for the CapabilityMap
54 */
55 public void setClient(Client client)
56 {
57 this.client = client;
58 }
59
60 /***
61 Returns the Client for the CapabilityMap
62 */
63 public Client getClient()
64 {
65 return this.client;
66 }
67
68 /***
69 Add capability to the CapabilityMap
70 */
71 public void addCapability(Capability capability)
72 {
73 if (capability != null)
74 this.capabilityMap.put(capability.getName(), capability);
75 }
76
77 /***
78 Add Mimetype to the MimetypeMap
79 */
80 public void addMimetype(MimeType mimetype)
81 {
82 if (mimetype != null)
83 this.mimeTypeMap.put(mimetype.getName(), mimetype);
84 }
85
86 /***
87 Add MediaType to the MediaTypeMap
88 */
89 public void addMediaType(MediaType mediatype)
90 {
91 if (mediatype != null)
92 this.mediaTypeMap.put(mediatype.getName(), mediatype);
93 }
94
95 /***
96 Returns the preferred MIME type for the current user-agent
97 */
98 public MimeType getPreferredType()
99 {
100
101 int prefMimeTypeId = this.client.getPreferredMimeTypeId();
102
103 MimeType mt = null;
104 Iterator e = this.mimeTypeMap.values().iterator();
105 while (e.hasNext())
106 {
107 mt = (MimeType) e.next();
108
109 if (mt.getMimetypeId() == prefMimeTypeId)
110 return mt;
111 }
112 log.error("Could not find preferred Mime Type for " + prefMimeTypeId);
113
114
115 return null;
116 }
117
118 /***
119 * Sets the preferred MediaType for this CapabilityMap
120 * @param MediaTypeEntry
121 */
122 public void setPreferredMediaType(MediaType type)
123 {
124 this.preferredMediaType = type;
125 }
126
127 /***
128 Returns the preferred media type for the current user-agent
129 */
130 public MediaType getPreferredMediaType()
131 {
132 return this.preferredMediaType;
133 }
134
135 /***
136 * Returns an ordered list of supported media-types, from most preferred
137 * to least preferred
138 */
139 public Iterator listMediaTypes()
140 {
141 return mediaTypeMap.values().iterator();
142 }
143
144 /***
145 Returns the user-agent string
146 */
147 public String getAgent()
148 {
149 return this.useragent;
150 }
151
152 /***
153 * set userAgent
154 */
155 public void setAgent(String userAgent)
156 {
157 this.useragent = userAgent;
158 }
159
160 /***
161 * Checks to see if the current agent has the specified capability
162 */
163 public boolean hasCapability(int capability)
164 {
165 Iterator capabilities = capabilityMap.values().iterator();
166 while (capabilities.hasNext())
167 {
168 if (((Capability) capabilities.next()).getCapabilityId()
169 == capability)
170 {
171 return true;
172 }
173 }
174 return false;
175 }
176
177 /***
178 * Checks to see if the current agent has the specified capability
179 */
180 public boolean hasCapability(String capability)
181 {
182 Iterator capabilities = capabilityMap.values().iterator();
183 while (capabilities.hasNext())
184 {
185 if (((Capability) capabilities.next()).getName().equals(capability))
186 {
187 return true;
188 }
189 }
190 return false;
191 }
192
193 /***
194 Get the mime types that this CapabilityMap supports.
195 */
196 public Iterator getMimeTypes()
197 {
198 return mimeTypeMap.values().iterator();
199 }
200
201 /***
202 Return true if this CapabilityMap supports the given MimeType
203 */
204 public boolean supportsMimeType(MimeType mimeType)
205 {
206 Iterator mimetypes = mimeTypeMap.values().iterator();
207 while (mimetypes.hasNext())
208 {
209 if (((MimeType) mimetypes.next()).getName().equals(mimeType.getName()))
210 {
211 return true;
212 }
213 }
214 return false;
215 }
216
217 /***
218 * Return true if this CapabilityMap supports the given media type
219 *
220 * @param media the name of a media type registered in the
221 * MediaType registry
222 *
223 * @return true is the capabilities of this agent at least match those
224 * required by the media type
225 */
226 public boolean supportsMediaType(String media)
227 {
228 Iterator mediatypes = mediaTypeMap.values().iterator();
229 while (mediatypes.hasNext())
230 {
231 if (((MediaType) mediatypes.next()).getName() == media)
232 {
233 return true;
234 }
235 }
236 return false;
237 }
238
239 /***
240 * Create a map -> string representation
241 */
242 public String toString()
243 {
244 return "";
245 }
246
247 }