001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.so6;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.core.FileData;
038:
039: import org.libresource.so6.core.command.Command;
040: import org.libresource.so6.core.command.text.AddBlock;
041: import org.libresource.so6.core.command.text.DelBlock;
042: import org.libresource.so6.core.engine.OpVectorFsImpl;
043: import org.libresource.so6.core.engine.PatchFile;
044: import org.libresource.so6.core.engine.util.FileUtils;
045: import org.libresource.so6.server.ls.LibresourceSynchronizerConstants;
046: import org.libresource.so6.server.ls.PatchBean;
047: import org.libresource.so6.server.ls.ejb.model.SynchronizerResourceValue;
048: import org.libresource.so6.server.ls.interfaces.LibresourceSynchronizerService;
049:
050: import org.libresource.web.Controller;
051: import org.libresource.web.config.Config;
052: import org.libresource.web.rss.Feed;
053: import org.libresource.web.rss.Item;
054:
055: import java.io.File;
056: import java.io.FileOutputStream;
057: import java.io.InputStream;
058:
059: import java.net.URI;
060:
061: import java.util.Collection;
062: import java.util.Iterator;
063: import java.util.ListIterator;
064:
065: import javax.servlet.http.HttpServletRequest;
066: import javax.servlet.http.HttpServletResponse;
067:
068: public class RssPatchOfSynchronizerController implements Controller {
069: public Object process(URI uri, HttpServletRequest request,
070: HttpServletResponse response) throws Exception {
071: LibresourceSynchronizerService libresourceSycnhronizerService = (LibresourceSynchronizerService) Libresource
072: .getService(LibresourceSynchronizerConstants.SERVICE);
073: SynchronizerResourceValue synchronizer = libresourceSycnhronizerService
074: .getSynchronizer(uri);
075: PatchBean[] patchList = libresourceSycnhronizerService
076: .listSynchronizerPatch(uri, 0);
077:
078: // max items in feed
079: int max;
080:
081: try {
082: max = Integer.parseInt(request.getParameter("max"));
083: } catch (Exception e) {
084: max = 10;
085: }
086:
087: // create feed
088: Feed feed = new Feed(synchronizer.getUri(), synchronizer
089: .getName(), synchronizer.getDescription());
090:
091: // create items
092: Item[] items;
093:
094: if (patchList.length < 10) {
095: items = new Item[patchList.length];
096: } else {
097: items = new Item[max];
098: }
099:
100: for (int i = 0; i < items.length; i++) {
101: FileData patch = libresourceSycnhronizerService.getPatch(
102: uri, patchList[i].getFromTicket(), patchList[i]
103: .getToTicket());
104: String content = "<pre>"
105: + getPatchContent(patch.getInputStream())
106: + "</pre>";
107: URI itemUri = new URI(synchronizer.getUri() + "/from"
108: + patchList[i].getFromTicket() + "to"
109: + patchList[i].getToTicket() + "");
110:
111: //"?view=viewPatch&from=" + patchList[i].getFromTicket() + "&to=" + patchList[i].getToTicket());
112: String itemTitle = patchList[i].getComment();
113:
114: if ((itemTitle == null) || (itemTitle.trim().length() == 0)) {
115: itemTitle = "(no comment)";
116: }
117:
118: items[i] = new Item(itemUri, itemTitle, patchList[i]
119: .getWsName(), patchList[i].getCreationDate(),
120: content);
121: }
122:
123: // redirect to generic feed page
124: request.setAttribute("feed", feed);
125: request.setAttribute("items", items);
126:
127: return "/pages/resource.rdf.jsp";
128: }
129:
130: private String getPatchContent(InputStream is) {
131: try {
132: File temp = File.createTempFile("viewpatch", ".xml", Config
133: .getInstance(null).getLsWebBuffer());
134: FileOutputStream os = new FileOutputStream(temp);
135: int readCount = -1;
136: byte[] buffer = new byte[1000000];
137:
138: while ((readCount = is.read(buffer)) > 0) {
139: os.write(buffer, 0, readCount);
140: }
141:
142: os.close();
143:
144: File workingDir = FileUtils.createTmpDir();
145: File cmds = new File(workingDir, "cmds");
146: cmds.mkdirs();
147:
148: File attach = new File(workingDir, "attach");
149: attach.mkdirs();
150:
151: OpVectorFsImpl opVector = new OpVectorFsImpl(cmds.getPath());
152: PatchFile patchFile = new PatchFile(temp.getAbsolutePath());
153: patchFile.buildOpVector(opVector, attach.getPath(), null);
154:
155: ListIterator commands = opVector.getCommands();
156: StringBuffer b = new StringBuffer();
157:
158: while (commands.hasNext()) {
159: Command cmd = (Command) commands.next();
160: b.append(cmd.toString() + "\n");
161:
162: if (cmd instanceof DelBlock) {
163: DelBlock delBlock = (DelBlock) cmd;
164: Collection oldContent = delBlock.getOldContent();
165:
166: for (Iterator iter = oldContent.iterator(); iter
167: .hasNext();) {
168: b.append(" -- " + (String) iter.next() + "\n");
169: }
170: }
171:
172: if (cmd instanceof AddBlock) {
173: AddBlock addBlock = (AddBlock) cmd;
174: Collection oldContent = addBlock.getContent();
175:
176: for (Iterator iter = oldContent.iterator(); iter
177: .hasNext();) {
178: b.append(" ++ " + (String) iter.next() + "\n");
179: }
180: }
181:
182: b.append("\n\n");
183: }
184:
185: return b.toString();
186: } catch (Exception e) {
187: return "Unable to get patch content...";
188: }
189: }
190: }
|