KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > content > display > component > UIStaticContentTab


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.content.display.component;
6
7 import java.io.FileInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.util.Locale JavaDoc;
11
12 import javax.faces.context.ExternalContext;
13 import javax.faces.context.FacesContext;
14 import javax.faces.context.ResponseWriter;
15 import javax.servlet.ServletContext JavaDoc;
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 /**
28  * @author benjaminmestrallet
29  */

30 public class UIStaticContentTab extends UIContentTab {
31
32   private ExoCache cache_;
33   
34   public UIStaticContentTab(ContentConfig config) throws Exception JavaDoc {
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 JavaDoc {
41     ResponseWriter w = context.getResponseWriter();
42     w.write("<div class='UIContentTab'>");
43     PortalResources appres = (PortalResources) SessionContainer
44         .getComponent(PortalResources.class);
45     Locale JavaDoc locale = appres.getLocale();
46     String JavaDoc uri = config_.getUri();
47     String JavaDoc content = null;
48     if (uri == null) {
49       content = "";
50     } else if (uri.endsWith(".html")) {
51       try {
52         content = resolveContent(uri);
53       } catch (Exception JavaDoc e) {
54       }
55     } else {
56       String JavaDoc tmpUri = uri + "_" + locale.getLanguage() + ".html";
57       try {
58         content = resolveContent(tmpUri);
59       } catch (Exception JavaDoc ex) {
60         tmpUri = uri + "_en.html";
61         try {
62           content = resolveContent(tmpUri);
63         } catch (Exception JavaDoc 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 JavaDoc resolveContent(String JavaDoc uri) throws Exception JavaDoc {
76     String JavaDoc content = (String JavaDoc) cache_.get(uri);
77     if(content != null)
78       return content;
79     if (uri.startsWith("war:")) {
80       String JavaDoc path = uri.substring(4, uri.length());
81       String JavaDoc[] pathElements = StringUtils.split(path, "/");
82       InputStream JavaDoc 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 JavaDoc 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 JavaDoc();
98       byte[] buf = new byte[is.available()];
99       is.read(buf);
100       content = new String JavaDoc(buf, config_.getEncoding());
101     } else if (uri.startsWith("file://")) {
102       String JavaDoc path = uri.substring(7, uri.length());
103       FileInputStream JavaDoc is = new FileInputStream JavaDoc(path);
104       if(is == null)
105         throw new IOException JavaDoc();
106       byte[] buf = new byte[is.available()];
107       is.read(buf);
108       content = new String JavaDoc(buf, config_.getEncoding());
109     }
110     cache_.put(uri, content);
111     return content;
112   }
113
114 }
Popular Tags