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 java.util.Collection;
20 import java.util.Hashtable;
21 import java.util.Iterator;
22 import java.util.Properties;
23 import java.util.Vector;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.jetspeed.capabilities.Capabilities;
28 import org.apache.jetspeed.capabilities.CapabilitiesException;
29 import org.apache.jetspeed.capabilities.Capability;
30 import org.apache.jetspeed.capabilities.CapabilityMap;
31 import org.apache.jetspeed.capabilities.Client;
32 import org.apache.jetspeed.capabilities.MediaType;
33 import org.apache.jetspeed.capabilities.MimeType;
34 import org.apache.jetspeed.capabilities.UnableToBuildCapabilityMapException;
35 import org.apache.jetspeed.components.dao.InitablePersistenceBrokerDaoSupport;
36 import org.apache.ojb.broker.query.Criteria;
37 import org.apache.ojb.broker.query.QueryByCriteria;
38 import org.apache.ojb.broker.query.QueryFactory;
39 import org.springframework.beans.BeansException;
40 import org.springframework.beans.factory.BeanFactory;
41 import org.springframework.beans.factory.BeanFactoryAware;
42
43 /***
44 * Jetspeed Capabilities
45 *
46 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
47 * @author <a href="mailto:roger.ruttimann@earthlink.net">Roger Ruttimann</a>
48 * @version $Id: JetspeedCapabilities.java 517124 2007-03-12 08:10:25Z ate $
49 */
50 public class JetspeedCapabilities extends InitablePersistenceBrokerDaoSupport implements Capabilities ,BeanFactoryAware
51 {
52 private static final Log log =
53 LogFactory.getLog(JetspeedCapabilities.class);
54
55 public static final String DEFAULT_AGENT = "Mozilla/4.0";
56
57 public static final String AGENT_XML = "agentxml/1.0";
58
59
60 Hashtable capabilityMapCache = new Hashtable();
61
62 private Collection clients = null;
63
64 /***
65 * added support for bean factory to create profile rules
66 */
67 private BeanFactory beanFactory;
68
69 /*** named bean references */
70 private String clientBeanName;
71 private String capabilityBeanName;
72 private String mimeTypeBeanName;
73 private String mediaTypeBeanName;
74
75 private Class clientClass;
76 private Class capabilityClass;
77 private Class mimeTypeClass;
78 private Class mediaTypeClass;
79
80
81 public JetspeedCapabilities(String repositoryPath, String clientBeanName, String mediaTypeBeanName, String mimeTypeBeanName, String capabilityBeanName)
82 {
83 super(repositoryPath);
84 this.clientBeanName = clientBeanName;
85 this.capabilityBeanName = capabilityBeanName;
86 this.mimeTypeBeanName = mimeTypeBeanName;
87 this.mediaTypeBeanName = mediaTypeBeanName;
88 }
89
90 /***
91 * Create a JetspeedProfiler with properties. Expected properties are:
92 *
93 * defaultRule = the default profiling rule
94 * anonymousUser = the name of the anonymous user
95 * persistenceStoreName = The name of the persistence persistenceStore component to connect to
96 * services.profiler.locator.impl = the pluggable Profile Locator impl
97 * services.profiler.principalRule.impl = the pluggable Principal Rule impl
98 * services.profiler.profilingRule.impl = the pluggable Profiling Rule impl
99 *
100 * @param persistenceStore The persistence persistenceStore
101 * @param properties Properties for this component described above
102 * @deprecated As of release 2.1, property-based class references replaced
103 * by container managed bean factory
104 */
105 public JetspeedCapabilities(String repositoryPath, Properties properties)
106 {
107 super(repositoryPath);
108 }
109
110
111
112
113
114
115 public void setBeanFactory(BeanFactory beanFactory) throws BeansException
116 {
117 this.beanFactory = beanFactory;
118 }
119
120
121 private Class getClientClass() throws ClassNotFoundException
122 {
123 if (clientClass == null)
124 {
125 clientClass = createClient(null).getClass();
126 }
127 return clientClass;
128 }
129
130 private Class getMimeTypeClass() throws ClassNotFoundException
131 {
132 if (mimeTypeClass == null)
133 {
134 mimeTypeClass = this.createMimeType(null).getClass();
135 }
136 return mimeTypeClass;
137 }
138 private Class getCapabilityClass()throws ClassNotFoundException
139 {
140 if (capabilityClass == null)
141 {
142 capabilityClass = this.createCapability(null).getClass();
143 }
144 return capabilityClass;
145 }
146
147 private Class getMediaTypeClass()throws ClassNotFoundException
148 {
149 if (mediaTypeClass == null)
150 {
151 mediaTypeClass = this.createMediaType(null).getClass();
152 }
153 return mediaTypeClass;
154 }
155
156
157 /***
158 * @param userAgent Agent from the request
159 * @throws UnableToBuildCapabilityMapException
160 * @see org.apache.jetspeed.services.capability.CapabilityService#getCapabilityMap(java.lang.String)
161 */
162 public CapabilityMap getCapabilityMap(String userAgent) throws UnableToBuildCapabilityMapException
163 {
164 CapabilityMap map = null;
165 boolean bClientFound = false;
166 String defaultAgent = null;
167
168 if (userAgent == null)
169 {
170 userAgent = DEFAULT_AGENT;
171 }
172
173
174
175 map = (CapabilityMap) capabilityMapCache.get(userAgent);
176
177 if (map != null)
178 {
179
180 return map;
181 }
182
183 while (!bClientFound)
184 {
185 Client entry = findClient(userAgent);
186
187 if (entry == null)
188 {
189 if (userAgent.equals(DEFAULT_AGENT))
190 {
191 log.error(
192 "CapabilityMap: Default agent not found in Client Registry !");
193
194
195 bClientFound = true;
196 } else
197 {
198
199 if (log.isDebugEnabled())
200 {
201 log.debug(
202 "CapabilityMap: useragent "
203 + userAgent
204 + "unknown, falling back to default");
205 }
206
207
208 defaultAgent = userAgent;
209 userAgent = DEFAULT_AGENT;
210 }
211 } else
212 {
213
214 map = new CapabilityMapImpl();
215
216
217 map.setClient(entry);
218
219
220 Iterator capabilities = entry.getCapabilities().iterator();
221 while (capabilities.hasNext())
222 {
223 map.addCapability((Capability) capabilities.next());
224 }
225
226 Collection mediatypes =
227 getMediaTypesForMimeTypes(entry.getMimetypes().iterator());
228
229
230 Iterator mimetypes = entry.getMimetypes().iterator();
231 while (mimetypes.hasNext())
232 {
233 map.addMimetype((MimeType) mimetypes.next());
234 }
235
236 Iterator media = mediatypes.iterator();
237 while (media.hasNext())
238 {
239 map.addMediaType((MediaType) media.next());
240 }
241
242
243 MediaType mtEntry =
244 getMediaTypeForMimeType(map.getPreferredType().getName());
245
246 map.setPreferredMediaType(mtEntry);
247
248
249 capabilityMapCache.put(userAgent, map);
250 if (defaultAgent != null)
251 capabilityMapCache.put(defaultAgent, map);
252 return map;
253 }
254
255 }
256
257 if(map != null)
258 {
259 return map;
260 }
261 else
262 {
263 throw new UnableToBuildCapabilityMapException("We were unable to build a capability map for the agent, "+userAgent+
264 ". This might be an indiciation that the capability database has not been correctly initialized.");
265 }
266 }
267
268 /***
269 * Returns the client which matches the given useragent string.
270 *
271 * @param useragent the useragent to match
272 * @return the found client or null if the user-agent does not match any
273 * defined client
274 * @see org.apache.jetspeed.capabilities.CapabilityService#findClient(java.lang.String)
275 */
276
277 public Client findClient(String userAgent)
278 {
279 Client clientEntry = null;
280 Iterator clients = getClients();
281
282 if (log.isDebugEnabled())
283 {
284 log.debug(
285 "ClientRegistry: Looking for client with useragent :"
286 + userAgent);
287 }
288
289 while (clients.hasNext())
290 {
291 Client client = (Client) clients.next();
292 if (client.getUserAgentPattern() != null)
293 {
294 try
295 {
296
297 String exp = client.getUserAgentPattern();
298
299
300
301 if (userAgent.matches(exp))
302 {
303
304 if (log.isDebugEnabled())
305 {
306 log.debug(
307 "Client: "
308 + userAgent
309 + " matches "
310 + client.getUserAgentPattern());
311 }
312
313 return client;
314 } else
315 {
316 if (log.isDebugEnabled())
317 {
318 log.debug(
319 "Client: "
320 + userAgent
321 + " does not match "
322 + client.getUserAgentPattern());
323 }
324 }
325 } catch (java.util.regex.PatternSyntaxException e)
326 {
327 String message =
328 "CapabilityServiceImpl: UserAgentPattern not valid : "
329 + client.getUserAgentPattern()
330 + " : "
331 + e.getMessage();
332 log.error(message, e);
333 }
334 }
335 }
336
337 return clientEntry;
338 }
339
340
341
342
343 public Iterator getClients()
344 {
345 if (null == clients)
346 {
347 try
348 {
349 QueryByCriteria query = QueryFactory.newQuery(getClientClass(), new Criteria());
350 query.addOrderByAscending("evalOrder");
351 this.clients = getPersistenceBrokerTemplate().getCollectionByQuery(query);
352 }
353 catch (Exception e)
354 {
355 String message =
356 "CapabilityServiceImpl: getClients query used invalid class ";
357 log.error(message, e);
358 return null;
359 }
360 }
361
362 return this.clients.iterator();
363 }
364
365
366
367
368 public Collection getMediaTypesForMimeTypes(Iterator mimetypes)
369 {
370
371
372 Criteria filter = new Criteria();
373
374 Vector temp = new Vector();
375
376 while (mimetypes.hasNext())
377 {
378 MimeType mt = (MimeType) mimetypes.next();
379
380
381
382
383
384 temp.add(mt.getName());
385 }
386
387 Collection co = null;
388 if (temp.size() > 0)
389 {
390 try
391 {
392 filter.addIn("mimetypes.name", temp);
393 QueryByCriteria query = QueryFactory.newQuery(getMediaTypeClass(), filter);
394 co = getPersistenceBrokerTemplate().getCollectionByQuery(query);
395 }
396 catch (Exception e)
397 {
398 String message =
399 "CapabilityServiceImpl: getMediaTypesForMimeTypes -> getMediaTypeClass query used invalid class ";
400 log.error(message, e);
401
402 }
403 }
404 if (co == null || co.isEmpty())
405 {
406 MediaType mt = getMediaType("html");
407 Vector v = new Vector();
408 v.add(mt);
409 return v;
410 }
411 return co;
412 }
413
414
415
416
417 public void deleteCapabilityMapCache()
418 {
419 capabilityMapCache.clear();
420 clients = null;
421 }
422
423
424
425
426 public MediaType getMediaType(String mediaType)
427 {
428 try
429 {
430 Criteria filter = new Criteria();
431 filter.addEqualTo("name", mediaType);
432 QueryByCriteria query = QueryFactory.newQuery(getMediaTypeClass(), filter);
433 return (MediaType) getPersistenceBrokerTemplate().getObjectByQuery(query);
434 }
435 catch (Exception e)
436 {
437 String message =
438 "CapabilityServiceImpl: getMediaType query used invalid class ";
439 log.error(message, e);
440 return null;
441 }
442 }
443
444 /***
445 * getMediaTypeForMimeType
446 * @param mimeType to use for lookup
447 * @return MediaTypeEntry that matches the lookup in the MEDIATYPE_TO_MIMETYPE table
448 */
449 public MediaType getMediaTypeForMimeType(String mimeTypeName)
450 {
451
452 Collection mediaTypeCollection = null;
453 try
454 {
455 Criteria filter = new Criteria();
456 filter.addEqualTo("mimetypes.name", mimeTypeName);
457
458 QueryByCriteria query = QueryFactory.newQuery(getMediaTypeClass(), filter);
459 mediaTypeCollection = getPersistenceBrokerTemplate().getCollectionByQuery(query);
460 }
461 catch (Exception e)
462 {
463 String message =
464 "CapabilityServiceImpl: getMediaTypeForMimeType query used invalid class ";
465 log.error(message, e);
466 return null;
467 }
468
469 Iterator mtIterator = mediaTypeCollection.iterator();
470 if (mtIterator.hasNext())
471 {
472 return (MediaType) mtIterator.next();
473 } else
474 {
475 return null;
476 }
477 }
478
479 /***
480 * Obtain an iterator of all existing capabilities.
481 * @return Returns an iterator for all existing Capabilities of type <code>Capability</code>
482 */
483 public Iterator getCapabilities()
484 {
485 QueryByCriteria query = null;
486 try
487 {
488 query = QueryFactory.newQuery(getCapabilityClass(), new Criteria());
489 }
490 catch (Exception e)
491 {
492 String message =
493 "CapabilityServiceImpl: getCapabilities query used invalid class ";
494 log.error(message, e);
495 return null;
496 }
497 query.addOrderByAscending("name");
498 return getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator();
499 }
500
501 /***
502 * Obtain an iterator of all existing mime types.
503 * @return Returns an iterator for all existing Mime Types of type <code>MimeType</code>
504 */
505 public Iterator getMimeTypes()
506 {
507 try
508 {
509 QueryByCriteria query = QueryFactory.newQuery(getMimeTypeClass(), new Criteria());
510 query.addOrderByAscending("name");
511 return getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator();
512 }
513 catch (Exception e)
514 {
515 String message =
516 "CapabilityServiceImpl: getMimeTypes query used invalid class ";
517 log.error(message, e);
518 return null;
519 }
520 }
521
522 /***
523 * Obtain an iterator of all existing media types.
524 * @return Returns an iterator for all existing media types of type <code>MediaType</code>
525 */
526 public Iterator getMediaTypes()
527 {
528 try
529 {
530 QueryByCriteria query = QueryFactory.newQuery(getMediaTypeClass(), new Criteria());
531 query.addOrderByAscending("name");
532 return getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator();
533 }
534 catch (Exception e)
535 {
536 String message =
537 "CapabilityServiceImpl: getMediaTypes query used invalid class ";
538 log.error(message, e);
539 return null;
540 }
541 }
542
543
544
545 public String getMimeTypeBeanName() {
546 return mimeTypeBeanName;
547 }
548
549
550
551
552 public void setMimeTypeBeanName(String mimeTypeBeanName) {
553 this.mimeTypeBeanName = mimeTypeBeanName;
554 }
555
556
557
558
559 public String getClientBeanName() {
560 return clientBeanName;
561 }
562
563
564
565
566 public void setClientBeanName(String clientBeanName) {
567 this.clientBeanName = clientBeanName;
568 }
569
570
571
572
573 public String getMediaTypeBeanName() {
574 return mediaTypeBeanName;
575 }
576
577
578
579
580 public void setMediaTypeBeanName(String mediaTypeBeanName) {
581 this.mediaTypeBeanName = mediaTypeBeanName;
582 }
583
584
585
586
587 public String getCapabilityBeanName() {
588 return capabilityBeanName;
589 }
590
591
592
593
594 public void setCapabilityBeanName(String capabilityBeanName) {
595 this.capabilityBeanName = capabilityBeanName;
596 }
597
598
599
600
601 public MimeType createMimeType(String mimeType)
602 throws ClassNotFoundException
603 {
604 MimeType mimeTypeobj = null;
605 if (mimeType != null)
606 {
607
608 mimeTypeobj = this.getMimeType(mimeType);
609 if (mimeTypeobj != null)
610 return mimeTypeobj;
611 }
612 try
613 {
614 mimeTypeobj = (MimeType) beanFactory.getBean(
615 this.mimeTypeBeanName, MimeType.class);
616 mimeTypeobj.setName(mimeType);
617 return mimeTypeobj;
618 } catch (Exception e)
619 {
620 log.error("Failed to create capability instance for " + this.mimeTypeBeanName
621 + " error : " + e.getLocalizedMessage());
622 throw new ClassNotFoundException("Spring failed to create the " + this.mimeTypeBeanName
623 + " mimeType bean.", e);
624 }
625 }
626
627
628
629
630
631 public Capability createCapability(String capabilityName) throws ClassNotFoundException
632 {
633 Capability capability = null;
634 if (capabilityName != null)
635 {
636
637 capability = this.getCapability(capabilityName);
638 if (capability != null)
639 return capability;
640 }
641 try
642 {
643 capability = (Capability) beanFactory.getBean(
644 this.capabilityBeanName, Capability.class);
645 capability.setName(capabilityName);
646 return capability;
647 } catch (Exception e)
648 {
649 log.error("Failed to create capability instance for " + this.capabilityBeanName
650 + " error : " + e.getLocalizedMessage());
651 throw new ClassNotFoundException("Spring failed to create the "
652 + " capability bean.", e);
653 }
654 }
655
656
657
658
659 public MediaType createMediaType(String mediaTypeName) throws ClassNotFoundException
660 {
661 MediaType mediaType = null;
662 if (mediaTypeName != null)
663 {
664
665 mediaType = this.getMediaType(mediaTypeName);
666 if (mediaType != null)
667 return mediaType;
668 }
669 try
670 {
671 mediaType = (MediaType) beanFactory.getBean(
672 this.mediaTypeBeanName, MediaType.class);
673 mediaType.setName(mediaTypeName);
674 return mediaType;
675 } catch (Exception e)
676 {
677 log.error("Failed to create mediaType instance for " + this.mediaTypeBeanName
678 + " error : " + e.getLocalizedMessage());
679 throw new ClassNotFoundException("Spring failed to create the "
680 + " mediaType bean.", e);
681 }
682 }
683
684
685
686
687
688 public Client createClient(String clientName) throws ClassNotFoundException
689 {
690 Client client = null;
691 if (clientName != null)
692 {
693
694 client = this.getClient(clientName);
695 if (client != null)
696 return client;
697 }
698 try
699 {
700 client = (Client) beanFactory.getBean(
701 this.clientBeanName, Client.class);
702 client.setName(clientName);
703 return client;
704 } catch (Exception e)
705 {
706 log.error("Failed to create client instance for " + this.clientBeanName
707 + " error : " + e.getLocalizedMessage());
708 throw new ClassNotFoundException("Spring failed to create the "
709 + " client bean.", e);
710 }
711 }
712
713
714
715 public MimeType getMimeType(String mimeType)
716 {
717 try
718 {
719 Criteria filter = new Criteria();
720 filter.addEqualTo("name", mimeType);
721 QueryByCriteria query = QueryFactory.newQuery(getMimeTypeClass(), filter);
722 return (MimeType) getPersistenceBrokerTemplate().getObjectByQuery(query);
723 }
724 catch (Exception e)
725 {
726 String message =
727 "MimeTypeserviceImpl: getCapability - query for getCapabilityClass failed ";
728 log.error(message, e);
729 return null;
730
731 }
732
733 }
734
735
736
737
738
739 public Client getClient(String clientName)
740 {
741 try
742 {
743 Criteria filter = new Criteria();
744 filter.addEqualTo("name", clientName);
745 QueryByCriteria query = QueryFactory.newQuery(getClientClass(), filter);
746 return (Client) getPersistenceBrokerTemplate().getObjectByQuery(query);
747 }
748 catch (Exception e)
749 {
750 String message =
751 "MimeTypeserviceImpl: getClient - query for getClientClass failed ";
752 log.error(message, e);
753 return null;
754
755 }
756 }
757
758
759
760
761
762 public Capability getCapability(String capability)
763 {
764 try
765 {
766
767 Criteria filter = new Criteria();
768 filter.addEqualTo("name", capability);
769 QueryByCriteria query = QueryFactory.newQuery(getCapabilityClass(), filter);
770 return (Capability) getPersistenceBrokerTemplate().getObjectByQuery(query);
771 }
772 catch (Exception e)
773 {
774 String message =
775 "MimeTypeserviceImpl: getCapability - query for getCapabilityClass failed ";
776 log.error(message, e);
777 return null;
778
779 }
780 }
781
782
783
784
785
786
787
788 public void storeMediaType(MediaType mediaType) throws CapabilitiesException
789 {
790
791
792 getPersistenceBrokerTemplate().store(mediaType);
793 }
794
795
796
797
798
799
800 public void deleteMediaType(MediaType mediaType)
801 throws CapabilitiesException
802 {
803
804 getPersistenceBrokerTemplate().delete(mediaType);
805 }
806
807
808
809
810
811
812
813 public void storeCapability(Capability capability) throws CapabilitiesException
814 {
815
816
817 getPersistenceBrokerTemplate().store(capability);
818 }
819
820
821
822
823
824
825 public void deleteCapability(Capability capability)
826 throws CapabilitiesException
827 {
828
829 getPersistenceBrokerTemplate().delete(capability);
830 }
831
832
833
834
835
836
837 public void storeMimeType(MimeType mimeType) throws CapabilitiesException
838 {
839
840
841 getPersistenceBrokerTemplate().store(mimeType);
842 }
843
844
845
846
847
848
849 public void deleteMimeType(MimeType mimeType)
850 throws CapabilitiesException
851 {
852
853 getPersistenceBrokerTemplate().delete(mimeType);
854 }
855
856
857
858
859
860
861
862
863
864 public void storeClient(Client client) throws CapabilitiesException
865 {
866
867
868 getPersistenceBrokerTemplate().store(client);
869 }
870
871
872
873
874
875
876 public void deleteClient(Client client)
877 throws CapabilitiesException
878 {
879
880 getPersistenceBrokerTemplate().delete(client);
881 }
882
883 }