1 23 package org.infoglue.cms.applications.managementtool.actions; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.InputStream ; 28 import java.util.ArrayList ; 29 import java.util.Enumeration ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Properties ; 33 34 import org.apache.commons.httpclient.HttpClient; 35 import org.apache.commons.httpclient.HttpMethod; 36 import org.apache.commons.httpclient.StatusLine; 37 import org.apache.commons.httpclient.methods.GetMethod; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.apache.pluto.om.common.Preference; 41 import org.apache.pluto.om.common.PreferenceSet; 42 import org.apache.pluto.om.entity.PortletApplicationEntityList; 43 import org.apache.pluto.om.portlet.PortletApplicationDefinition; 44 import org.apache.pluto.om.portlet.PortletDefinition; 45 import org.apache.pluto.om.portlet.PortletDefinitionList; 46 import org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistry; 47 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction; 48 import org.infoglue.cms.controllers.kernel.impl.simple.PortletAssetController; 49 import org.infoglue.cms.entities.content.DigitalAsset; 50 import org.infoglue.cms.entities.content.DigitalAssetVO; 51 import org.infoglue.cms.exception.SystemException; 52 import org.infoglue.cms.util.CmsPropertyHandler; 53 import org.infoglue.cms.util.NotificationMessage; 54 import org.infoglue.cms.util.RemoteCacheUpdater; 55 import org.infoglue.deliver.portal.deploy.Deploy; 56 import org.infoglue.deliver.portal.om.PortletApplicationEntityImpl; 57 import org.infoglue.deliver.portal.om.PortletApplicationEntityListImpl; 58 import org.infoglue.deliver.portal.om.PortletEntityImpl; 59 import org.infoglue.deliver.portal.om.PreferenceImpl; 60 import org.infoglue.deliver.portal.om.PreferenceSetImpl; 61 62 import webwork.action.ActionContext; 63 import webwork.multipart.MultiPartRequestWrapper; 64 65 71 public class UploadPortletAction extends InfoGlueAbstractAction 72 { 73 private static final Log log = LogFactory.getLog(UploadPortletAction.class); 74 75 private static final String PORTLET_DEPLOY_PREFIX = "portlet.deploy"; 76 77 private DigitalAsset digitalAsset; 78 79 public String doExecute() { 81 try 82 { 83 MultiPartRequestWrapper mpr = ActionContext.getMultiPartRequest(); 84 if (mpr == null) 85 { 86 return "input"; 87 } 88 89 log.debug("Handling upload..."); 90 Enumeration names = mpr.getFileNames(); 91 if (names.hasMoreElements()) 92 { 93 String name = (String ) names.nextElement(); 94 log.debug("name:" + name); 95 File uploadedFile = mpr.getFile(name); 96 if (uploadedFile == null || uploadedFile.length() == 0) 97 { 98 log.error("No file found in multipart request"); 99 return "input"; 100 } 101 102 String contentType = mpr.getContentType(name); 103 String fileName = mpr.getFilesystemName(name); 104 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 105 log.debug("fileName:" + fileName); 106 107 String appName = fileName; 109 int dot = appName.lastIndexOf("."); 110 if (dot > 0) 111 { 112 appName = appName.substring(0, dot); 113 } 114 115 log.info("appName:" + appName); 116 117 File file = new File (uploadedFile.getParentFile(), "tmp" + System.currentTimeMillis()); 120 log.info("file:" + file.getAbsolutePath()); 121 PortletApplicationDefinition pad = Deploy.prepareArchive(uploadedFile, file, appName); 122 123 log.info("Adding portlet application to registry: " + appName); 126 PortletApplicationEntityImpl pae = new PortletApplicationEntityImpl(); 127 pae.setId(appName); 128 129 PortletDefinitionList pdl = pad.getPortletDefinitionList(); 130 for (Iterator it = pdl.iterator(); it.hasNext();) 131 { 132 PortletDefinition pd = (PortletDefinition) it.next(); 133 log.debug("Adding portlet: " + pd.getName()); 134 PortletEntityImpl pe = new PortletEntityImpl(); 135 pe.setId(pd.getName()); 136 137 ArrayList destPrefs = new ArrayList (); 139 PreferenceSet prefSet = pd.getPreferenceSet(); 140 for (Iterator prefs = prefSet.iterator(); prefs.hasNext();) 141 { 142 Preference src = (Preference) prefs.next(); 143 ArrayList destValues = new ArrayList (); 144 for (Iterator values = src.getValues(); values.hasNext();) 145 { 146 destValues.add(values.next()); 147 } 148 destPrefs.add(new PreferenceImpl(src.getName(), destValues)); 149 } 150 pe.setPreferenceSet(new PreferenceSetImpl(destPrefs)); 151 pae.addPortletEntity(pe); 152 } 153 154 log.debug("Creating Digital Asset..."); 156 DigitalAssetVO newAsset = new DigitalAssetVO(); 157 newAsset.setAssetContentType(contentType); 158 newAsset.setAssetKey(fileName); 159 newAsset.setAssetFileName(fileName); 160 newAsset.setAssetFilePath(filePath); 161 newAsset.setAssetFileSize(new Integer (new Long (file.length()).intValue())); 162 163 List assets = PortletAssetController.getDigitalAssetByName(fileName); 165 if (assets != null && assets.size() > 0) 166 { 167 log.info("Removing old instance of " + fileName); 168 for (Iterator it = assets.iterator(); it.hasNext();) 169 { 170 DigitalAsset oldAsset = (DigitalAsset) it.next(); 171 PortletAssetController.delete(oldAsset.getId()); 172 } 173 } 174 175 log.info("Storing Digital Asset (portlet) " + fileName); 176 InputStream is = new FileInputStream (file); 177 digitalAsset = PortletAssetController.create(newAsset, is); 178 is.close(); 179 log.debug("Digital Asset stored as id=" + digitalAsset.getId()); 180 181 uploadedFile.delete(); 183 file.delete(); 184 185 PortletApplicationEntityList pael = PortletEntityRegistry.getPortletApplicationEntityList(); 189 if (pael instanceof PortletApplicationEntityListImpl) 190 { 191 ((PortletApplicationEntityListImpl) pael).add(pae); 192 log.debug("Portlet application successfully added to registry"); 193 } else 194 { 195 log.error("Unknown implementation of PortletApplicationEntityList, " 196 + "cannot add portlet application!"); 197 return "error"; 198 } 199 PortletEntityRegistry.store(); 200 201 updateDeliverEngines(digitalAsset.getId()); 203 } 204 else 205 { 206 throw new SystemException("No file was uploaded..."); 207 } 208 } 209 catch (Throwable e) 210 { 211 log.error("ERROR", e); 212 return "error"; 213 } 214 215 return "success"; 216 } 217 218 224 private void updateDeliverEngines(Integer digitalAssetId) 225 { 226 List allUrls = CmsPropertyHandler.getInternalDeliveryUrls(); 227 allUrls.addAll(CmsPropertyHandler.getPublicDeliveryUrls()); 228 229 Iterator urlIterator = allUrls.iterator(); 230 while(urlIterator.hasNext()) 231 { 232 String url = (String )urlIterator.next() + "/DeployPortlet.action"; 233 234 try 235 { 236 HttpClient client = new HttpClient(); 237 238 client.setConnectionTimeout(5000); 240 241 HttpMethod method = new GetMethod(url); 243 method.setQueryString("digitalAssetId=" + digitalAssetId); 244 method.setFollowRedirects(true); 245 246 client.executeMethod(method); 248 StatusLine status = method.getStatusLine(); 249 if (status != null && status.getStatusCode() == 200) { 250 log.info("Successfully deployed portlet at " + url); 251 } else { 252 log.warn("Failed to deploy portlet at " + url + ": " + status); 253 } 254 255 method.releaseConnection(); 257 } 258 catch(Exception e) 259 { 260 e.printStackTrace(); 261 } 262 } 263 264 298 299 } 300 301 304 public DigitalAsset getDigitalAsset() { 305 return digitalAsset; 306 } 307 308 } | Popular Tags |