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  package org.apache.jetspeed.modules.actions.portlets;
17  
18  // Jetspeed classes
19  import org.apache.jetspeed.modules.actions.portlets.security.SecurityConstants;
20  import org.apache.jetspeed.om.registry.ClientEntry;
21  import org.apache.jetspeed.om.registry.RegistryEntry;
22  import org.apache.jetspeed.portal.portlets.VelocityPortlet;
23  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
24  import org.apache.jetspeed.services.logging.JetspeedLogger;
25  import org.apache.jetspeed.services.Registry;
26  
27  // Regexp classes
28  import org.apache.regexp.RE;
29  import org.apache.regexp.RESyntaxException;
30  
31  // Trubine classes
32  import org.apache.turbine.util.DynamicURI;
33  import org.apache.turbine.util.RunData;
34  import org.apache.velocity.context.Context;
35  
36  /***
37   * This action enables to update the client entries
38   *
39   * @author <a href="mailto:caius1440@hotmail.com">Jeremy Ford</a>
40   * @version $Id: ClientUpdateAction.java,v 1.4 2004/02/23 02:56:58 jford Exp $
41   */
42  public class ClientUpdateAction extends RegistryUpdateAction
43  {
44      private static final String whoAmI = "ClientUpdateAction: ";
45      private static final String CLIENT_UPDATE_PANE = "ClientForm";
46      
47      /***
48       * Static initialization of the logger for this class
49       */    
50      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(ClientUpdateAction.class.getName());
51      
52      public ClientUpdateAction()
53      {
54          registryEntryName = "client_name";
55          registry = Registry.CLIENT;
56          pane = CLIENT_UPDATE_PANE;
57      }
58  
59  	/***
60  	 * Subclasses must override this method to provide default behavior
61  	 * for the portlet action
62  	 */
63  	/***
64  	 * Build the normal state content for this portlet.
65  	 *
66  	 * @param portlet The velocity-based portlet that is being built.
67  	 * @param context The velocity context for this request.
68  	 * @param rundata The turbine rundata context for this request.
69  	 */
70  	protected void buildNormalContext(
71  		VelocityPortlet portlet,
72  		Context context,
73  		RunData rundata)
74  		throws Exception
75  	{
76          super.buildNormalContext(portlet, context, rundata);
77          
78  		String mode =
79  			rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
80  
81  		if (mode != null
82  			&& (mode.equals(SecurityConstants.PARAM_MODE_DELETE)
83  				|| mode.equals(SecurityConstants.PARAM_MODE_UPDATE)))
84  		{
85  			String clientName =
86  				rundata.getParameters().getString("client_name");
87  			ClientEntry clientEntry =
88  				(ClientEntry) Registry.getEntry(Registry.CLIENT, clientName);
89  			context.put("entry", clientEntry);
90  		}
91  	}
92  
93  	/***
94  	 * Add a mimetype to a client entry
95  	 * @param rundata The turbine rundata context for this request.
96  	 * @param context The velocity context for this request.
97  	 * @throws Exception
98  	 */
99  	public void doAddmimetype(RunData rundata, Context context)
100 		throws Exception
101 	{
102 		try
103 		{
104 			String clientName =
105 				rundata.getParameters().getString("client_name");
106 			ClientEntry clientEntry =
107 				(ClientEntry) Registry.getEntry(Registry.CLIENT, clientName);
108 			if (clientEntry != null)
109 			{
110                 String mimeType = rundata.getParameters().getString("mime_type");
111 				clientEntry.getMimetypeMap().addMimetype(mimeType);
112 
113 				Registry.addEntry(Registry.CLIENT, clientEntry);
114 				clearUserData(rundata);
115 			}
116 			else
117 			{
118                 DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_INVALID_ENTITY_NAME);
119 				rundata.setRedirectURI(duri.toString());
120 				resetForm(rundata);
121                 
122                 logger.error(this.getClass().getName() + ": Failed to find registry entry while trying to add mime type");
123 			}
124 		}
125 		catch (Exception e)
126 		{
127             DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_UPDATE_FAILED);
128 			rundata.setRedirectURI(duri.toString());
129 			resetForm(rundata);
130             
131             logger.error("Error addin mime type", e);
132 		}
133 	}
134     
135 
136 	/***
137 	 * Remove mime types from a client entry
138 	 * @param rundata The turbine rundata context for this request.
139 	 * @param context The velocity context for this request.
140 	 * @throws Exception
141 	 */
142     
143 	public void doRemovemimetype(RunData rundata, Context context)
144 		throws Exception
145 	{
146 		try
147 		{
148 			String clientName =
149 				rundata.getParameters().getString("client_name");
150 			ClientEntry clientEntry =
151 				(ClientEntry) Registry.getEntry(Registry.CLIENT, clientName);
152 			if (clientEntry != null)
153 			{
154 				String[] mimeTypes =
155                     rundata.getParameters().getStrings("mime_type");
156 				if (mimeTypes != null && mimeTypes.length > 0)
157 				{
158 					for (int i = 0; i < mimeTypes.length; i++)
159 					{
160 						String mimeType = mimeTypes[i];
161 
162 						clientEntry.getMimetypeMap().removeMimetype(mimeType);
163 					}
164 
165 					Registry.addEntry(Registry.CLIENT, clientEntry);
166 					clearUserData(rundata);
167 				}
168 			}
169 			else
170 			{
171                 DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_INVALID_ENTITY_NAME);
172 				rundata.setRedirectURI(duri.toString());
173 				resetForm(rundata);
174                 
175                 logger.error(this.getClass().getName() + ": Failed to find registry entry while trying to remove mime types");
176 			}
177 		}
178 		catch (Exception e)
179 		{
180             DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_UPDATE_FAILED);
181 			rundata.setRedirectURI(duri.toString());
182 			resetForm(rundata);
183             
184             logger.error("Error removing mime types", e);
185 		}
186 	}
187     
188 
189 	/***
190      * @see org.apache.jetspeed.modules.actions.portlets.RegistryUpdateAction#updateRegistryEntry(org.apache.turbine.util.RunData, org.apache.jetspeed.om.registry.RegistryEntry)
191      */
192     protected void updateRegistryEntry(RunData rundata, RegistryEntry registryEntry) throws Exception
193 	{
194         super.updateRegistryEntry(rundata, registryEntry);
195         
196         updateClientEntry(rundata, (ClientEntry)registryEntry);
197 	}
198 
199 	/***
200 	 * Set the client entry parameters from the input parameters
201 	 * @param rundata The turbine rundata context for this request.
202 	 * @param context The velocity context for this request.
203 	 */
204 	private void updateClientEntry(
205 		RunData rundata,
206         ClientEntry clientEntry) throws Exception
207 	{
208 		String userAgentPattern =
209 			rundata.getParameters().getString("user_agent_pattern");
210 		String manufacturer = rundata.getParameters().getString("manufacturer");
211 		String model = rundata.getParameters().getString("model");
212 		String version = rundata.getParameters().getString("version");
213 
214         if(hasChanged(clientEntry.getUseragentpattern(), userAgentPattern))
215 		{
216             try
217 			{
218                 RE re = new RE(userAgentPattern);
219                 clientEntry.setUseragentpattern(userAgentPattern);
220 			}
221             catch(RESyntaxException e)
222 			{
223                 logger.error(whoAmI + "Illegal regular expression syntax " + userAgentPattern + " for user agent");
224                 logger.debug(whoAmI + "Illegal regular expression syntax for user agent", e);
225                 
226                 throw new IllegalArgumentException("Illegal regular expression syntax for user agent");
227 			}
228 		}
229         if(hasChanged(clientEntry.getManufacturer(), manufacturer))
230 		{
231             clientEntry.setManufacturer(manufacturer);
232         }
233         if(hasChanged(clientEntry.getModel(), model))
234         {
235             clientEntry.setModel(model);
236         }
237         if(hasChanged(clientEntry.getVersion(), version))
238         {
239             clientEntry.setVersion(version);
240 		}
241 	}
242 
243 	/***
244 	 * Populates the user's temp storage with form data
245 	 * @param rundata The turbine rundata context for this request.
246 	 */
247     protected void resetForm(RunData rundata)
248 	{
249         super.resetForm(rundata);
250         
251 		String userAgentPattern =
252 			rundata.getParameters().getString("user_agent_pattern");
253 		String manufacturer = rundata.getParameters().getString("manufacturer");
254 		String model = rundata.getParameters().getString("model");
255 		String version = rundata.getParameters().getString("version");
256 
257 		String capability = rundata.getParameters().getString("capability");
258         String mimeType = rundata.getParameters().getString("mime_type");
259 
260 		rundata.getUser().setTemp("user_agent_pattern", userAgentPattern);
261 		rundata.getUser().setTemp("manufacturer", manufacturer);
262 		rundata.getUser().setTemp("model", model);
263 		rundata.getUser().setTemp("version", version);
264 
265 		rundata.getUser().setTemp("capability", capability);
266 		rundata.getUser().setTemp("mimetype", mimeType);
267 	}
268 
269 	/***
270 	 * Clears the temporary storage of any data that was used
271 	 * @param rundata The turbine rundata context for this request.
272 	 */
273     protected void clearUserData(RunData rundata)
274     {
275 	try
276 	{
277             super.clearUserData(rundata);
278             
279             rundata.getUser().removeTemp("user_agent_pattern");
280             rundata.getUser().removeTemp("manufacturer");
281             rundata.getUser().removeTemp("model");
282             rundata.getUser().removeTemp("version");
283 
284             rundata.getUser().removeTemp("capability");
285             rundata.getUser().removeTemp("mime_type");
286         }
287         catch (Exception e)
288         {
289                    
290             if (logger.isDebugEnabled())
291             {
292                 logger.debug("ClientUpdateAction: Failed to clear user data");
293             }
294         }
295     }
296 }