View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * 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 and
15   * limitations under the License.
16   */
17  
18  package org.apache.jetspeed.capabilities.impl;
19  
20  import java.util.Map;
21  import java.util.Iterator;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.apache.jetspeed.capabilities.Capabilities;
27  import org.apache.jetspeed.capabilities.CapabilityMap;
28  import org.apache.jetspeed.capabilities.MediaType;
29  import org.apache.jetspeed.capabilities.Client;
30  import org.apache.jetspeed.pipeline.PipelineException;
31  import org.apache.jetspeed.pipeline.valve.AbstractValve;
32  import org.apache.jetspeed.pipeline.valve.ValveContext;
33  import org.apache.jetspeed.request.RequestContext;
34  
35  /***
36   * Invokes the capability customizer in the request pipeline
37   * 
38   * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
39   * @version $Id: CapabilityCustomizerValveImpl.java 517719 2007-03-13 15:05:48Z ate $
40   */
41  public class CapabilityCustomizerValveImpl extends AbstractValve
42  {
43  
44      private static final Log log = LogFactory.getLog(CapabilityCustomizerValveImpl.class);
45  
46      private Capabilities capabilities;
47      private Map clientToMediaTypeMap;
48  
49      public CapabilityCustomizerValveImpl( Capabilities capabilities, Map clientToMediaTypeMap )
50      {
51          this.capabilities = capabilities;
52          this.clientToMediaTypeMap = clientToMediaTypeMap;
53      }
54  
55      /***
56       * Initialize the valve before using in a pipeline.
57       */
58      public void initialize() throws PipelineException
59      {
60  
61      }
62  
63      public void invoke( RequestContext request, ValveContext context ) throws PipelineException
64      {
65          CapabilityMap cm = request.getCapabilityMap();
66  
67          if (cm != null && this.clientToMediaTypeMap != null)
68          {
69              Client client = cm.getClient();
70              String mediaTypeName = (String) this.clientToMediaTypeMap.get(client.getName());
71              
72              if (mediaTypeName != null)
73              {
74                  MediaType mediaType = this.capabilities.getMediaType(mediaTypeName);
75                  cm.setPreferredMediaType(mediaType);
76                  request.setMediaType(mediaTypeName);
77              }
78          }
79  
80          // Pass control to the next Valve in the Pipeline
81          context.invokeNext(request);
82      }
83  
84      public String toString()
85      {
86          return "CapabilityCustomizerValveImpl";
87      }
88  }