1 16 package org.apache.cocoon.portal.coplet.adapter.impl; 17 18 import java.io.UnsupportedEncodingException ; 19 import java.util.Enumeration ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.cocoon.ProcessingException; 24 import org.apache.cocoon.components.ContextHelper; 25 import org.apache.cocoon.environment.ObjectModelHelper; 26 import org.apache.cocoon.environment.Request; 27 import org.apache.cocoon.portal.coplet.CopletData; 28 import org.apache.cocoon.portal.coplet.CopletFactory; 29 import org.apache.cocoon.portal.coplet.CopletInstanceData; 30 import org.apache.cocoon.portal.event.CopletInstanceEvent; 31 import org.apache.cocoon.portal.event.impl.CopletLinkEvent; 32 import org.apache.cocoon.portal.layout.CompositeLayout; 33 import org.apache.cocoon.portal.layout.Item; 34 import org.apache.cocoon.portal.layout.LayoutFactory; 35 import org.apache.cocoon.portal.layout.NamedItem; 36 import org.apache.cocoon.portal.layout.impl.CopletLayout; 37 import org.apache.cocoon.portal.profile.ProfileManager; 38 import org.apache.cocoon.portal.transformation.ProxyTransformer; 39 import org.apache.cocoon.util.NetUtils; 40 import org.apache.cocoon.xml.XMLUtils; 41 import org.xml.sax.ContentHandler ; 42 import org.xml.sax.SAXException ; 43 44 52 public class ApplicationCopletAdapter extends CachingURICopletAdapter { 53 54 57 public void streamContent(final CopletInstanceData coplet, 58 final String uri, 59 final ContentHandler contentHandler) 60 throws SAXException { 61 try { 62 super.streamContent(coplet, uri, contentHandler); 63 } catch (SAXException se) { 64 this.getLogger().error( 65 "ApplicationCopletAdapter: Exception while getting coplet resource", 66 se); 67 68 this.renderErrorContent(coplet, contentHandler); 69 } 70 } 71 72 78 public void handleCopletInstanceEvent(CopletInstanceEvent e) { 79 super.handleCopletInstanceEvent(e); 80 81 if ( e instanceof CopletLinkEvent ) { 82 CopletLinkEvent event = (CopletLinkEvent) e; 83 CopletInstanceData coplet = (CopletInstanceData) event.getTarget(); 84 String link = event.getLink(); 85 86 if ("createNewCopletInstance".equals(link)) { 87 try { 88 createNewInstance(coplet); 89 } catch (ProcessingException ex) { 90 getLogger().error("Could not create new coplet instance", ex); 91 } 92 } else { 93 String linkValue = event.getLink(); 96 Boolean addParams = (Boolean )this.getConfiguration(coplet, "appendParameters", Boolean.FALSE); 97 if ( addParams.booleanValue() ) { 98 final StringBuffer uri = new StringBuffer (event.getLink()); 99 boolean hasParams = (uri.toString().indexOf("?") != -1); 100 101 final Map objectModel = ContextHelper.getObjectModel(this.context); 103 final Request r = ObjectModelHelper.getRequest(objectModel); 104 final Enumeration params = r.getParameterNames(); 105 while (params.hasMoreElements()) { 106 final String name = (String )params.nextElement(); 107 if (!name.startsWith("cocoon-portal-")) { 108 final String [] values = r.getParameterValues(name); 109 for(int i = 0; i < values.length; i++) { 110 if ( hasParams ) { 111 uri.append('&'); 112 } else { 113 uri.append('?'); 114 hasParams = true; 115 } 116 uri.append(name); 117 uri.append('='); 118 try { 119 uri.append(NetUtils.decode(values[i], "utf-8")); 120 } catch (UnsupportedEncodingException uee) { 121 } 123 } 124 } 125 } 126 linkValue = uri.toString(); 127 } 128 coplet.setTemporaryAttribute(ProxyTransformer.LINK, linkValue); 129 } 130 } 131 } 132 133 139 private void createNewInstance(CopletInstanceData coplet) 140 throws ProcessingException { 141 ProfileManager profileManager = null; 142 try { 143 profileManager = 144 (ProfileManager) this.manager.lookup(ProfileManager.ROLE); 145 146 CopletData copletData = coplet.getCopletData(); 147 148 LayoutFactory lfac = 149 (LayoutFactory) this.manager.lookup(LayoutFactory.ROLE); 150 151 CopletLayout copletLayout = 152 (CopletLayout) lfac.newInstance("coplet"); 153 154 CopletFactory cfac = 155 (CopletFactory) manager.lookup(CopletFactory.ROLE); 156 157 CopletInstanceData newCoplet = cfac.newInstance(copletData); 158 159 copletLayout.setCopletInstanceData(newCoplet); 160 profileManager.register(copletLayout); 161 162 NamedItem newItem = new NamedItem(); 163 newItem.setLayout(copletLayout); 164 165 CompositeLayout tabLayout = 166 (CompositeLayout) profileManager.getPortalLayout( 167 "portalApplications", null); 168 169 newItem.setName(getNewInstanceTabName(tabLayout)); 170 tabLayout.addItem(newItem); 171 } catch (ServiceException ce) { 172 throw new ProcessingException( 173 "Unable to lookup profile manager.", 174 ce); 175 } catch (Exception e) { 176 throw new ProcessingException(e); 177 } finally { 178 this.manager.release(profileManager); 179 } 180 } 181 182 186 private String getNewInstanceTabName(CompositeLayout layout) { 187 Integer data = (Integer ) layout.getAspectData("tab"); 188 Item selectedItem = (NamedItem) layout.getItem(data.intValue()); 189 190 if (selectedItem instanceof NamedItem) { 191 return ((NamedItem) selectedItem).getName(); 192 } 193 return ("New"); 194 } 195 196 203 protected boolean renderErrorContent(CopletInstanceData coplet, 204 ContentHandler handler) 205 throws SAXException { 206 handler.startDocument(); 207 XMLUtils.startElement(handler, "p"); 208 XMLUtils.data( 209 handler, 210 "ApplicationCopletAdapter: Can't get content for coplet " 211 + coplet.getId() 212 + ". Look up the logs."); 213 XMLUtils.endElement(handler, "p"); 214 handler.endDocument(); 215 216 return true; 217 } 218 219 } 220 | Popular Tags |