View Javadoc

1   /*
2    * Copyright 2000-2001,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  
17  package org.apache.jetspeed.capability;
18  
19  //standard Jetspeed stuff
20  import org.apache.jetspeed.util.MimeType;
21  import org.apache.jetspeed.om.registry.ClientEntry;
22  import org.apache.jetspeed.om.registry.MediaTypeEntry;
23  import org.apache.jetspeed.om.registry.MediaTypeRegistry;
24  import org.apache.jetspeed.services.Registry;
25  
26  //standard Java stuff
27  import java.util.Vector;
28  import java.util.Iterator;
29  import java.util.Enumeration;
30  
31  /***
32   * Read only wrapper around a ClientEntry registry entry that
33   * implements the CapabilityMap interface
34   *
35   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
36   * @version $Id: BaseCapabilityMap.java,v 1.8 2004/02/23 02:46:39 jford Exp $
37   */
38  public class BaseCapabilityMap implements CapabilityMap
39  {
40  
41      private String      useragent;
42      private ClientEntry entry;
43  
44      protected BaseCapabilityMap(String agent, ClientEntry entry)
45      {
46          this.useragent = agent;
47          this.entry = entry;
48      }
49  
50      /***
51      @see CapabilityMap#getPreferredType
52      */
53      public MimeType getPreferredType()
54      {
55          return entry.getMimetypeMap().getPreferredMimetype();
56      }
57  
58      /***
59      Returns the preferred media type for the current user-agent
60      */
61      public String getPreferredMediaType()
62      {
63          Iterator i = listMediaTypes();
64  
65          if (i.hasNext())
66          {
67              return (String)i.next();
68          }
69  
70          return null;
71      }
72  
73      /***
74       * Returns an ordered list of supported media-types, from most preferred
75       * to least preferred
76       */
77      public Iterator listMediaTypes()
78      {
79          Vector results = new Vector();
80          Vector types = new Vector();
81  
82          // first copy the current media type list, ordered by global preference
83          Enumeration en = ((MediaTypeRegistry)Registry.get(Registry.MEDIA_TYPE)).getEntries();
84          while (en.hasMoreElements())
85          {
86              types.add(en.nextElement());
87          }
88  
89          //then retrieve a list of supported mime-types, ordered by
90          //preference
91  
92          Iterator mimes = entry.getMimetypeMap().getMimetypes();
93  
94          //now, for each mime-type test if the media is supported
95          while(mimes.hasNext())
96          {
97              String mime = ((MimeType)mimes.next()).getContentType();
98              Iterator i = types.iterator();
99  
100             while(i.hasNext())
101             {
102                 MediaTypeEntry mte = (MediaTypeEntry)i.next();
103 
104                 if (mime.equals(mte.getMimeType()))
105                 {
106                     if (entry.getCapabilityMap().containsAll(mte.getCapabilityMap()))
107                     {
108                         results.add(mte.getName());
109                     }
110                 }
111             }
112         }
113 
114         return results.iterator();
115     }
116 
117     /***
118     @see CapabilityMap#getAgent
119     */
120     public String getAgent()
121     {
122         return this.useragent;
123     }
124 
125     /***
126     @see CapabilityMap#hasCapability
127     */
128     public boolean hasCapability( int cap )
129     {
130         return false;
131     }
132 
133     /***
134     @see CapabilityMap#hasCapability
135     */
136     public boolean hasCapability( String capability )
137     {
138         Iterator i = entry.getCapabilityMap().getCapabilities();
139 
140         while (i.hasNext())
141         {
142             String cap = (String)i.next();
143 
144             if (cap.equals(capability))
145             {
146                 return true;
147             }
148         }
149 
150         return false;
151     }
152 
153     /***
154     @see CapabilityMap#getMimeTypes
155     */
156     public MimeType[] getMimeTypes()
157     {
158         Vector v = new Vector();
159         Iterator i = entry.getMimetypeMap().getMimetypes();
160 
161         while (i.hasNext())
162         {
163             MimeType mime = (MimeType)i.next();
164             v.add(mime);
165         }
166 
167         return (MimeType[])v.toArray();
168     }
169 
170     /***
171     @see CapabilityMap#supportsMimeType
172     */
173     public boolean supportsMimeType( MimeType mimeType )
174     {
175         Iterator i = entry.getMimetypeMap().getMimetypes();
176 
177         while (i.hasNext())
178         {
179             MimeType mime = (MimeType)i.next();
180 
181             if (mime.equals(mimeType))
182             {
183                 return true;
184             }
185         }
186 
187         return false;
188 
189     }
190 
191     /***
192     @see CapabilityMap#supportsMimeType
193     */
194     public boolean supportsMediaType( String media )
195     {
196         if (media == null)
197         {
198             return true;
199         }
200 
201         MediaTypeEntry mte = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, media);
202 
203         if (!supportsMimeType(new MimeType(mte.getMimeType())))
204         {
205             return false;
206         }
207 
208         return entry.getCapabilityMap().containsAll(mte.getCapabilityMap());
209 
210     }
211 
212     /***
213     Create a map string representation
214     */
215     public String toString()
216     {
217         StringBuffer desc = new StringBuffer(entry.getName());
218 
219         Iterator i = entry.getMimetypeMap().getMimetypes();
220 
221         while (i.hasNext())
222         {
223             MimeType mime = (MimeType)i.next();
224             desc.append( mime ).append("-");
225         }
226 
227         i = entry.getCapabilityMap().getCapabilities();
228 
229         while ( i.hasNext() )
230         {
231           String capa = (String)i.next();
232           desc.append(capa).append("/");
233         }
234 
235         return desc.toString();
236     }
237 
238 }
239