001: package ru.emdev.EmForge.svn.web;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.IOException;
006: import java.util.Collection;
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: import javax.faces.context.FacesContext;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.tmatesoft.svn.core.SVNException;
014: import org.tmatesoft.svn.core.SVNProperty;
015:
016: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
017: import ru.emdev.EmForge.wiki.web.bean.Crumb;
018:
019: /**
020: * Subversion file viewer (svnfileviewer.xhtml) backing bean
021: *
022: * @author Walter Mourao
023: *
024: */
025: public class SvnFileViewerController extends SvnBaseController {
026:
027: public static final String TITLE = "File viewer";
028:
029: @Override
030: public MainMenuItem getSelectionItemOnMainMenu() {
031: return MainMenuItem.SOURCES;
032: }
033:
034: @Override
035: public String getTitleImpl() {
036: return TITLE;
037: }
038:
039: @Override
040: public Crumb getTrailCrumbInfo() {
041: return null;
042: }
043:
044: @Override
045: protected void init() {
046: super .init();
047:
048: fileContentLines = null;
049: resourceProperties = null;
050: commitMessage = null;
051: fileTextType = false;
052: binaryFileContent = null;
053: fileContent = null;
054: lastRevision = null;
055: size = 0;
056: strSize = null;
057: author = null;
058: age = null;
059: date = null;
060:
061: loadFileContent();
062: }
063:
064: //********* controller code ********//
065:
066: /**
067: * Fills the relevante properties
068: *
069: */
070: private void loadFileContent() {
071:
072: binaryFileContent = new ByteArrayOutputStream();
073:
074: final Map fileProperties = new HashMap();
075: final long fileRevision = Helper
076: .getCurrentRevision(getRevision());
077:
078: try {
079: getSvnRepository().getFile(getPath(), fileRevision,
080: fileProperties, binaryFileContent);
081: } catch (SVNException e) {
082: this .addErrorMessage("Subversion error", e
083: .getLocalizedMessage());
084: logger.error(e.getLocalizedMessage());
085: binaryFileContent = null;
086: return;
087: }
088:
089: mimeType = (String) fileProperties.get(SVNProperty.MIME_TYPE);
090: fileTextType = SVNProperty.isTextMimeType(mimeType);
091:
092: resourceProperties = new java.util.ArrayList<String[]>();
093: for (java.util.Iterator it = fileProperties.keySet().iterator(); it
094: .hasNext();) {
095: final String key = (String) it.next();
096:
097: if (Helper.isUserProperty(key))//filters only user properties.
098: resourceProperties.add(new String[] { key,
099: fileProperties.get(key).toString() });
100: }
101:
102: SvnDataVO[] svnDataArray = null;
103: try {
104: svnDataArray = Helper
105: .listEntries(getSvnRepository(), Helper
106: .dirPathFromFilePath(getPath()),
107: fileRevision);
108: } catch (SVNException e) {
109: this .addErrorMessage("Subversion error", e
110: .getLocalizedMessage());
111: logger.error(e.getLocalizedMessage(), e);
112: binaryFileContent = null;
113: return;
114: }
115:
116: SvnDataVO svnData = null;
117:
118: for (int i = 0; i < svnDataArray.length; i++) {
119: if (svnDataArray[i].getPath().equals(getPath())) {
120: svnData = svnDataArray[i];
121: break;
122: }
123: }
124:
125: if (svnData != null) {
126: commitMessage = svnData.getCommitMessage();
127: lastRevision = svnData.getRevision();
128: size = svnData.getSize();
129: strSize = svnData.getStrSize();
130: author = svnData.getAuthor();
131: age = svnData.getAge();
132: date = svnData.getDate();
133: } else {
134: logger.warn("Cannot get svn data for " + getPath()
135: + " revision " + fileRevision);
136: }
137: }
138:
139: /**
140: * Responses the "download" link
141: */
142: public void download() {
143: final FacesContext context = FacesContext.getCurrentInstance();
144:
145: final int contentLength = binaryFileContent.size();
146:
147: HttpServletResponse response = (HttpServletResponse) context
148: .getExternalContext().getResponse();
149: response.setContentType("application/octet-stream");
150: response.setContentLength(contentLength);
151: response.setHeader("Content-disposition", "inline; filename=\""
152: + Helper.fileNameFromFilePath(getPath()) + "\"");
153:
154: try {
155: // Write file contents to the response
156: final BufferedOutputStream output = new BufferedOutputStream(
157: response.getOutputStream());
158:
159: output.write(binaryFileContent.toByteArray());
160:
161: output.flush();
162: } catch (IOException e) {
163: this .addErrorMessage("Download error", e
164: .getLocalizedMessage());
165: logger.error(e.getLocalizedMessage(), e);
166: }
167:
168: context.responseComplete();
169: }
170:
171: //********* attributes ***********//
172:
173: /**
174: * The current file mime type
175: */
176: private String mimeType;
177:
178: public String getMimeType() {
179: return mimeType;
180: }
181:
182: /**
183: * The revision commit message
184: */
185: private String commitMessage;
186:
187: public String getCommitMessage() {
188: return commitMessage;
189: }
190:
191: /**
192: * Holds the file content
193: */
194: private ByteArrayOutputStream binaryFileContent;
195:
196: public ByteArrayOutputStream getBinaryFileContent() {
197: return binaryFileContent;
198: }
199:
200: /**
201: * Holds the file content in String format
202: */
203: private String fileContent;
204:
205: public String getFileContent() {
206: if (binaryFileContent == null)
207: return null;
208: else {
209: if (fileContent == null)
210: fileContent = binaryFileContent.toString();
211:
212: return fileContent;
213: }
214: }
215:
216: /**
217: * True if it is a text file.
218: */
219: private boolean fileTextType;
220:
221: public boolean isFileTextType() {
222: return fileTextType;
223: }
224:
225: /**
226: * The setted user properties
227: */
228: private Collection<String[]> resourceProperties;
229:
230: public Collection<String[]> getResourceProperties() {
231: return resourceProperties;
232: }
233:
234: /**
235: * The file content splitted into lines, if it is a text file, null if it is a binary file.
236: */
237: private String[] fileContentLines;
238:
239: public String[] getFileContentLines() {
240: if (getFileContent() != null)
241: fileContentLines = fileContent.split("\n");
242:
243: return fileContentLines;
244: }
245:
246: /**
247: * The file revision text info
248: */
249: String lastRevision;
250: long size;
251: String strSize;
252: String author;
253: String age;
254: java.util.Date date;
255:
256: public String getAge() {
257: return age;
258: }
259:
260: public String getAuthor() {
261: return author;
262: }
263:
264: public java.util.Date getDate() {
265: return date;
266: }
267:
268: public String getLastRevision() {
269: return lastRevision;
270: }
271:
272: public long getSize() {
273: return size;
274: }
275:
276: public String getStrSize() {
277: return strSize;
278: }
279: }
|