1 5 package org.exoplatform.portlets.content.display.component; 6 7 import java.io.FileInputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.util.Locale ; 11 12 import javax.faces.context.ExternalContext; 13 import javax.faces.context.FacesContext; 14 import javax.faces.context.ResponseWriter; 15 import javax.servlet.ServletContext ; 16 17 import org.apache.commons.lang.StringUtils; 18 import org.exoplatform.container.PortalContainer; 19 import org.exoplatform.container.SessionContainer; 20 import org.exoplatform.portal.session.PortalResources; 21 import org.exoplatform.portlets.content.display.component.model.ContentConfig; 22 import org.exoplatform.services.cache.CacheService; 23 import org.exoplatform.services.cache.ExoCache; 24 import org.exoplatform.services.portletcontainer.ExoPortletContext; 25 import org.exoplatform.services.portletcontainer.ExoPortletRequest; 26 27 30 public class UIStaticContentTab extends UIContentTab { 31 32 private ExoCache cache_; 33 34 public UIStaticContentTab(ContentConfig config) throws Exception { 35 super(config); 36 CacheService cacheS = (CacheService) PortalContainer.getComponent(CacheService.class); 37 cache_ = cacheS.getCacheInstance(getClass().getName()); 38 } 39 40 public void encodeChildren(FacesContext context) throws IOException { 41 ResponseWriter w = context.getResponseWriter(); 42 w.write("<div class='UIContentTab'>"); 43 PortalResources appres = (PortalResources) SessionContainer 44 .getComponent(PortalResources.class); 45 Locale locale = appres.getLocale(); 46 String uri = config_.getUri(); 47 String content = null; 48 if (uri == null) { 49 content = ""; 50 } else if (uri.endsWith(".html")) { 51 try { 52 content = resolveContent(uri); 53 } catch (Exception e) { 54 } 55 } else { 56 String tmpUri = uri + "_" + locale.getLanguage() + ".html"; 57 try { 58 content = resolveContent(tmpUri); 59 } catch (Exception ex) { 60 tmpUri = uri + "_en.html"; 61 try { 62 content = resolveContent(tmpUri); 63 } catch (Exception ex2) { 64 } 65 } 66 } 67 if (content == null) { 68 w.write("No configuration for the content"); 69 } else { 70 w.write(content); 71 } 72 w.write("</div>"); 73 } 74 75 private String resolveContent(String uri) throws Exception { 76 String content = (String ) cache_.get(uri); 77 if(content != null) 78 return content; 79 if (uri.startsWith("war:")) { 80 String path = uri.substring(4, uri.length()); 81 String [] pathElements = StringUtils.split(path, "/"); 82 InputStream is = null; 83 ExternalContext eContext = FacesContext.getCurrentInstance() 84 .getExternalContext(); 85 if ("content".equals(pathElements[0])) { 86 path = path.substring(2 + pathElements[0].length()); 87 is = eContext.getResourceAsStream(path); 88 } else { 89 ServletContext context = ((ExoPortletContext) (((ExoPortletRequest) eContext 90 .getRequest()).getPortletSession().getPortletContext())) 91 .getWrappedServletContext(); 92 context = context.getContext("/" + pathElements[0]); 93 path = path.substring(2 + pathElements[0].length()); 94 is = context.getResourceAsStream(path); 95 } 96 if(is == null) 97 throw new IOException (); 98 byte[] buf = new byte[is.available()]; 99 is.read(buf); 100 content = new String (buf, config_.getEncoding()); 101 } else if (uri.startsWith("file://")) { 102 String path = uri.substring(7, uri.length()); 103 FileInputStream is = new FileInputStream (path); 104 if(is == null) 105 throw new IOException (); 106 byte[] buf = new byte[is.available()]; 107 is.read(buf); 108 content = new String (buf, config_.getEncoding()); 109 } 110 cache_.put(uri, content); 111 return content; 112 } 113 114 } | Popular Tags |