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.plugin.misc;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.io.OutputStream;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.commons.io.CopyUtils;
29  import org.apache.commons.io.IOUtils;
30  import org.seasar.tuigwaa.cms.ContentsStoreService;
31  import org.seasar.tuigwaa.cms.core.CmsRequest;
32  import org.seasar.tuigwaa.cms.core.CmsResponse;
33  import org.seasar.tuigwaa.cms.core.Resource;
34  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
35  import org.seasar.tuigwaa.cms.core.wiki.base.WikiHelper;
36  import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
37  import org.seasar.tuigwaa.plugin.PluginException;
38  import org.seasar.tuigwaa.plugin.PluginRequest;
39  import org.seasar.tuigwaa.system.TgwSecurityException;
40  
41  import com.isenshi.util.CharUtil;
42  import com.isenshi.util.HtmlBuffer;
43  import com.isenshi.util.PathUtils;
44  import com.isenshi.util.ResourceUtils;
45  import com.isenshi.util.extlib.StrutsUtil;
46  
47  /***
48   * @author nishioka
49   */
50  public class WebAlbumPlugin extends AbstractTgwPlugin {
51  
52  	private static int DEFAULT_WIDTH = 200;
53  
54  	private static final int DEFAULT_COLUMNNUM = 3;
55  
56  	private static final String THUMBNAIL_PATH_PREFIX = "tmp/";
57  	
58  	private static final String THUMBNAIL_DIR_PREFIX = "thumbnail";
59  	
60  	
61  	private static final String PARAM_IMGURL = getParameterName("webalbum.img");
62  
63  	public String doHTMLView(CmsRequest req, CmsResponse res,
64  			PluginRequest prequest) throws PluginException {
65  
66  		String path = req.getPage().getResource().getPath();
67  		WikiContext context = getConfiguration().getWikiContext();
68  		
69  		List list = null;
70  		
71  		try {
72  			list = context.getAttachmentFileList(req, path);
73  		} catch (TgwSecurityException e) {
74  			return "";
75  		}
76  
77  		if (list == null) {
78  			return "";
79  		}
80  
81  		int width = DEFAULT_WIDTH;
82  		int columnNum = DEFAULT_COLUMNNUM;
83  
84  		String[] args = prequest.getArgs();
85  		if (existArg(args,0)) {
86  			columnNum = Integer.parseInt(args[0]);
87  			width = 600 / columnNum;
88  		}
89  
90  		HtmlBuffer buf = new HtmlBuffer();
91  
92  		buf.appendStartTag("table");
93  		buf.appendStartTag("tr");
94  
95  		int columnCount = 0;
96  		for (Iterator i = list.iterator(); i.hasNext();) {
97  			Resource resource = (Resource) i.next();
98  			if (WikiHelper.isImage(resource.getPageName())) {
99  				if (columnCount == columnNum) {
100 					buf.endTag();
101 					buf.appendStartTag("tr");
102 					columnCount = 0;
103 				}
104 				requireThumbnail(path, resource, width);
105 				String imgName = resource.getPageName();
106 				String link = 
107 					context.getAttachedFileURL(path,imgName,req).toString();				
108 				String thumbnailPath = createThumbnailPath(resource
109 						.getSiteName(), path, width, imgName);
110 				req.setParameter(PARAM_IMGURL, thumbnailPath);
111 				String thumbnailLink = context.getPluginProxyURL(
112 						prequest.getName(), req).toString();
113 
114 				bindImageTd(buf, link, thumbnailLink, imgName, width);
115 				columnCount++;
116 			}
117 		}
118 
119 		buf.endTag();
120 		buf.endTag();
121 
122 		return buf.toString();
123 	}
124 
125 	/***
126 	 * download thumbnail
127 	 */
128 	public String doAction(HttpServletRequest request, HttpServletResponse response) {
129 		
130 		String thumbnailPath = StrutsUtil.getURLDecodedParameter(request,PARAM_IMGURL);
131 		File file = ResourceUtils.getFile(thumbnailPath);
132 
133 		OutputStream os = null;
134 		FileInputStream fis = null;
135 		try {
136 			fis = new FileInputStream(file);			
137 			os = response.getOutputStream();
138 			CopyUtils.copy(fis, os);
139 		} catch (IOException e) {
140 			e.printStackTrace();
141 		} finally {
142 			IOUtils.closeQuietly(os);
143 			IOUtils.closeQuietly(fis);
144 		}
145 		return null;
146 	}
147 
148 	private void requireThumbnail(String pagePath, Resource resource, int width) {
149 		String extention = PathUtils.getExtension(resource.getPageName());
150 		String siteName = resource.getSiteName();
151 		String imgName = resource.getPageName();
152 		String path = createThumbnailPath(siteName, pagePath, width, imgName);
153 		File file = ResourceUtils.getFile(path);
154 		if (!file.exists()) {
155 			OutputStream os = null;
156 			try {
157 				os = ResourceUtils.getOutputStream(path);
158 				ContentsStoreService store = (ContentsStoreService) getService(ContentsStoreService.class);
159 				store.createThumbnail(resource, os, width, CharUtil.charpop(
160 						extention, '.'));
161 			} finally {
162 				IOUtils.closeQuietly(os);
163 			}
164 		}
165 	}
166 
167 	private String createThumbnailPath(String siteName, String pagePath,
168 			int width, String imageName) {		
169 		String thumbnailPath = THUMBNAIL_PATH_PREFIX + siteName + "/" + pagePath + "/" + THUMBNAIL_DIR_PREFIX + width + "/" + imageName;				
170 		return thumbnailPath;
171 	}
172 
173 	private void bindImageTd(HtmlBuffer buf, String origLink, String thumbLink,
174 			String alt, int width) {
175 		buf.appendStartTag("td");
176 		buf.appendStartTag("a");
177 		buf.appendAttribute("href", origLink);
178 		buf.appendImg(thumbLink, alt, width, null);
179 		buf.endTag();
180 		buf.endTag(); // td close
181 	}
182 }