View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.io.IOException;
22  import java.net.URL;
23  import java.util.Properties;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.springframework.extensions.webscripts.Description.RequiredAuthentication;
28  
29  
30  /**
31   * Presentation (web tier) Web Script Container
32   * 
33   * @author davidc
34   */
35  public class PresentationContainer extends AbstractRuntimeContainer
36  {
37      private static final Log logger = LogFactory.getLog(PresentationContainer.class);
38      
39  	/* (non-Javadoc)
40  	 * @see org.alfresco.web.scripts.RuntimeContainer#executeScript(org.alfresco.web.scripts.WebScriptRequest,
41       *      org.alfresco.web.scripts.WebScriptResponse, org.alfresco.web.scripts.Authenticator)
42  	 */
43      public void executeScript(WebScriptRequest scriptReq, WebScriptResponse scriptRes, Authenticator auth)
44          throws IOException
45      {
46          // Handle authentication of scripts on a case-by-case basis.
47          // Currently we assume that if a webscript servlet has any authenticator
48          // applied then it must be for some kind of remote user auth as supplied.
49          WebScript script = scriptReq.getServiceMatch().getWebScript();
50          Description desc = script.getDescription();
51          RequiredAuthentication required = desc.getRequiredAuthentication();
52          if (auth == null || RequiredAuthentication.none == required || auth.authenticate(required, false))
53          {
54              script.execute(scriptReq, scriptRes);
55          }
56      }
57  
58      /* (non-Javadoc)
59       * @see org.alfresco.web.scripts.Container#getDescription()
60       */
61      public ServerModel getDescription()
62      {
63          Properties props = null;
64          URL url = this.getClass().getClassLoader().getResource("version.properties");
65          if (url != null)
66          {
67              try
68              {
69                  props = new Properties();
70                  props.load(url.openStream());
71              }
72              catch (IOException err)
73              {
74                  logger.warn("Failed to load version properties: " + err.getMessage(), err);
75              }
76          }
77          return new PresentationServerModel(props);
78      }
79      
80      /**
81       * Presentation Tier Model
82       *
83       * @author davidc
84       */
85      private class PresentationServerModel implements ServerModel
86      {
87          private Properties props = null;
88          private String version = null;
89          
90          public PresentationServerModel(Properties props)
91          {
92              this.props = props;
93          }
94          
95          public String getContainerName()
96          {
97              return getName();
98          }
99  
100         public String getId()
101         {
102             return UNKNOWN;
103         }
104 
105         public String getName()
106         {
107             return UNKNOWN;
108         }
109 
110         public String getEdition()
111         {
112             return (props != null ? props.getProperty("version.edition") : UNKNOWN);
113         }
114 
115         public int getSchema()
116         {
117             return (props != null ? Integer.parseInt(props.getProperty("version.schema")) : -1);
118         }
119 
120         public String getVersion()
121         {
122             if (this.version == null)
123             {
124                 if (props != null)
125                 {
126                     StringBuilder version = new StringBuilder(getVersionMajor());
127                     version.append(".");
128                     version.append(getVersionMinor());
129                     version.append(".");
130                     version.append(getVersionRevision());
131                     
132                     String label = getVersionLabel();
133                     String build = getVersionBuild();
134                     
135                     boolean hasLabel = (label != null && label.length() > 0);
136                     boolean hasBuild = (build != null && build.length() > 0);
137                     
138                     // add opening bracket if either a label or build number is present
139                     if (hasLabel || hasBuild)
140                     {
141                        version.append(" (");
142                     }
143                     
144                     // add label if present
145                     if (hasLabel)
146                     {
147                        version.append(label);
148                     }
149                     
150                     // add build number is present
151                     if (hasBuild)
152                     {
153                        // if there is also a label we need a separating space
154                        if (hasLabel)
155                        {
156                           version.append(" ");
157                        }
158                        
159                        version.append(build);
160                     }
161                     
162                     // add closing bracket if either a label or build number is present
163                     if (hasLabel || hasBuild)
164                     {
165                        version.append(")");
166                     }
167                     
168                     this.version = version.toString();
169                 }
170                 else
171                 {
172                     this.version = UNKNOWN;
173                 }
174             }
175             return this.version;
176         }
177 
178         public String getVersionBuild()
179         {
180             return (props != null ? props.getProperty("version.build") : UNKNOWN);
181         }
182 
183         public String getVersionLabel()
184         {
185             return (props != null ? props.getProperty("version.label") : UNKNOWN);
186         }
187 
188         public String getVersionMajor()
189         {
190             return (props != null ? props.getProperty("version.major") : UNKNOWN);
191         }
192 
193         public String getVersionMinor()
194         {
195             return (props != null ? props.getProperty("version.minor") : UNKNOWN);
196         }
197 
198         public String getVersionRevision()
199         {
200             return (props != null ? props.getProperty("version.revision") : UNKNOWN);
201         }
202         
203         private final static String UNKNOWN = "<unknown>"; 
204     }
205 }