1 16 package org.apache.jetspeed.modules.actions.portlets; 17 18 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 import org.apache.regexp.RE; 29 import org.apache.regexp.RESyntaxException; 30 31 import org.apache.turbine.util.DynamicURI; 33 import org.apache.turbine.util.RunData; 34 import org.apache.velocity.context.Context; 35 36 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 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 63 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 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 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 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 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 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 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 } 297 | Popular Tags |