View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.view.wiki;
17  
18  import java.net.MalformedURLException;
19  import java.net.URL;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.seasar.framework.container.S2Container;
27  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
28  import org.seasar.tuigwaa.cms.ContentsStoreService;
29  import org.seasar.tuigwaa.cms.core.CmsRequest;
30  import org.seasar.tuigwaa.cms.core.Resource;
31  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
32  import org.seasar.tuigwaa.security.Action;
33  import org.seasar.tuigwaa.security.SecurityService;
34  import org.seasar.tuigwaa.system.Constants;
35  import org.seasar.tuigwaa.system.SiteConfig;
36  import org.seasar.tuigwaa.system.SiteService;
37  import org.seasar.tuigwaa.system.TgwSecurityException;
38  
39  import com.isenshi.util.CharUtil;
40  
41  /***
42   * @author someda
43   */
44  public class WikiContextImpl implements WikiContext {
45  
46  	private static final String CREATEPAGEURL = "/createNamedPage.do";
47  
48  	private static final String VIEWATTACHEMENTURL = "/viewAttachment.do";
49  
50  	private static final String PLUGINPROXYURL = "/plugin.do";
51  
52  	private static final String EDITPAGEURL = "/editPage.do";
53  
54  	private static ContentsStoreService contentsStoreService;
55  
56  	private static SecurityService securityService;
57  
58  	private static SiteService siteService;
59  
60  	private Log log_ = LogFactory.getLog(getClass());
61  
62  	public WikiContextImpl() {
63  		S2Container container = SingletonS2ContainerFactory.getContainer();
64  		contentsStoreService = (ContentsStoreService) container
65  				.getComponent(ContentsStoreService.class);
66  		securityService = (SecurityService) container
67  				.getComponent(SecurityService.class);
68  		siteService = (SiteService) container.getComponent(SiteService.class);
69  	}
70  
71  	public boolean isPageExist(String pagename, CmsRequest request)
72  			throws TgwSecurityException {
73  		return contentsStoreService.isExistResource(request.getSiteName(), pagename, false);
74  	}
75  
76  	public URL getURLByName(String pagename, CmsRequest request, Map params)
77  			throws TgwSecurityException {
78  
79  		SiteConfig siteConfig = siteService
80  				.getSiteConfig(request.getSiteName()); // to-use siteService
81  		// for PageForm Ajax
82  		Action action = Action.PAGE_VIEW.createParameteredAction(pagename);
83  
84  		if (!siteConfig.isSecurity()
85  				|| securityService.hasPermission(request.getSiteName(), action)) {
86  
87  			StringBuffer buf = new StringBuffer();
88  			boolean first = true;
89  			for (Iterator i = params.keySet().iterator(); i.hasNext();) {
90  				if(first){
91  					first = false;
92  				}else{
93  					buf.append("&");
94  				}
95  				String key = (String) i.next();
96  				buf.append(CharUtil.urlEncode(key) + "="
97  						+ CharUtil.urlEncode((String) params.get(key)));
98  			}
99  			return getURLByName(pagename, request, buf.toString());
100 		} else {
101 			throw new TgwSecurityException(
102 					"does not have enough permission to view this page.");
103 		}
104 	}
105 
106 	public URL getURLByName(String pagename, CmsRequest request)
107 			throws TgwSecurityException {
108 		return getURLByName(pagename, request, "");
109 	}
110 
111 	private URL getURLByName(String pagename, CmsRequest request,
112 			String extraQuery) {
113 		String module = "/" + request.getSiteName();
114 		String encodedpage = CharUtil.urlEncode(pagename);
115 		String file = request.getContextPath() + module + "/" + encodedpage;
116 		if(extraQuery!=null && extraQuery.length()> 0){
117 			file += "?" + extraQuery;
118 		}
119 		return getCompleteURL(file, request);
120 	}
121 
122 	public boolean hasCreatePermission(CmsRequest request) {
123 		SiteConfig siteConfig = siteService
124 				.getSiteConfig(request.getSiteName());
125 		return !siteConfig.isSecurity()
126 				|| securityService.hasPermission(request.getSiteName(),
127 						Action.PAGE_MANAGE);
128 	}
129 
130 	public URL getCreatePageURL(String pagename, CmsRequest request)
131 			throws TgwSecurityException {
132 
133 		SiteConfig siteConfig = siteService
134 				.getSiteConfig(request.getSiteName());
135 
136 		if (!siteConfig.isSecurity()
137 				|| securityService.hasPermission(request.getSiteName(),
138 						Action.PAGE_MANAGE)) {
139 
140 			String module = "/" + request.getSiteName();
141 			String encodedpage = CharUtil.urlEncode(pagename);
142 			String file = request.getContextPath() + module + CREATEPAGEURL
143 					+ "?" + Constants.PARAM_PAGENAME + "=" + encodedpage;
144 			return getCompleteURL(file, request);
145 		} else {
146 			throw new TgwSecurityException(
147 					"does not have enough permission to create new page.");
148 		}
149 
150 	}
151 
152 	public URL getEditPageURL(String pagename, CmsRequest request)
153 			throws TgwSecurityException {
154 
155 		SiteConfig siteConfig = siteService
156 				.getSiteConfig(request.getSiteName());
157 
158 		if (!siteConfig.isSecurity()
159 				|| securityService.hasPermission(request.getSiteName(),
160 						Action.PAGE_MANAGE)) {
161 			String module = "/" + request.getSiteName();
162 			String encodedpage = CharUtil.urlEncode(pagename);
163 			String file = request.getContextPath() + module + EDITPAGEURL + "?"
164 					+ Constants.PARAM_PAGENAME + "=" + encodedpage;
165 			return getCompleteURL(file, request);
166 		} else {
167 			throw new TgwSecurityException(
168 					"does not have enough permission to create new page.");
169 		}
170 	}
171 
172 	public URL getAttachedFileURL(String pagename, String filename,
173 			CmsRequest request) {
174 		String module = "/" + request.getSiteName();
175 		String encodedpage = CharUtil.urlEncode(pagename);
176 		String attachFileName = CharUtil.urlEncode(filename);
177 		String file = request.getContextPath() + module + VIEWATTACHEMENTURL
178 				+ "?" + Constants.PARAM_PAGENAME + "=" + encodedpage
179 				+ "&" + Constants.PARAM_FILE_NAME + "=" + attachFileName;
180 		return getCompleteURL(file, request);
181 	}
182 
183 	public List getAttachmentFileList(CmsRequest request, String pagePath)
184 			throws TgwSecurityException {
185 		String siteName = request.getSiteName();
186 		return contentsStoreService.getAttachmentFileList(siteName, pagePath);
187 	}
188 
189 	public List getRecentList(CmsRequest request, int size) throws Exception {
190 		return contentsStoreService.getLatestContents(request.getSiteName(),
191 				size);
192 	}
193 
194 	public List getDirectoryList(CmsRequest request, String folder)
195 			throws Exception {
196 		return contentsStoreService
197 				.getFolderList(request.getSiteName(), folder);
198 	}
199 
200 	public List getRecursiveDirectoryList(CmsRequest request, String folder)
201 			throws Exception {
202 		return contentsStoreService.getRecursiveFolderList(request
203 				.getSiteName(), folder);
204 	}
205 
206 	public List getResourceList(CmsRequest request, String path)
207 			throws Exception {
208 		return contentsStoreService.getPageOrFolderList(request.getSiteName(),
209 				path);
210 	}
211 
212 	public URL getPluginProxyURL(String pluginname, CmsRequest request) {
213 		String module = "/" + request.getSiteName();
214 		String file = request.getContextPath() + module + PLUGINPROXYURL + "?"
215 				+ Constants.PARAM_PLUGINNAME + "=" + pluginname;
216 		Iterator itr = request.getParamterNames();
217 		StringBuffer buf = new StringBuffer();
218 		while (itr.hasNext()) {
219 			String key = (String) itr.next();
220 			if (key.indexOf(pluginname) != -1)
221 				buf.append("&" + key + "="
222 						+ CharUtil.urlEncode(request.getParameter(key)));
223 		}
224 		file += buf.toString();
225 		return getCompleteURL(file, request);
226 	}
227 
228 	public URL getCompleteURL(String filepath, CmsRequest request) {
229 		URL url = null;
230 		try {
231 			String scheme = request.getScheme();
232 			String server = request.getServerName();
233 			int port = request.getServerPort();
234 			url = new URL(scheme, server, port, filepath);
235 		} catch (MalformedURLException mue) {
236 			log_.error("Cannot generate page url " + filepath);
237 		}
238 		return url;
239 	}
240 
241 	public Resource getResource(CmsRequest request, String pagePath) {
242 		String siteName = request.getSiteName();
243 		try{
244 			return contentsStoreService.getResource(siteName, pagePath);
245 		}catch(TgwSecurityException tse){
246 			return null;
247 		}
248 	}
249 
250 }