[kune-commits] r886 - in trunk: .
src/main/java/org/ourproject/kune/app/server
src/main/java/org/ourproject/kune/docs/client
src/main/java/org/ourproject/kune/docs/client/cnt
src/main/java/org/ourproject/kune/docs/client/cnt/reader
src/main/java/org/ourproject/kune/platf/server
src/main/java/org/ourproject/kune/platf/server/auth
src/main/java/org/ourproject/kune/platf/server/content
src/main/java/org/ourproject/kune/platf/server/manager/impl
src/main/java/org/ourproject/kune/platf/server/rpc
src/main/resources/db
src/test/java/org/ourproject/kune/platf/server/manager
src/test/java/org/ourproject/kune/platf/server/manager/impl
vjrj
vjrj at ourproject.org
Fri Sep 26 16:57:08 CEST 2008
Author: vjrj
Date: 2008-09-26 16:56:55 +0200 (Fri, 26 Sep 2008)
New Revision: 886
Added:
trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentUtils.java
trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileDownloadManager.java
Modified:
trunk/pom.xml
trunk/src/main/java/org/ourproject/kune/app/server/KuneRackModule.java
trunk/src/main/java/org/ourproject/kune/docs/client/DocumentClientModule.java
trunk/src/main/java/org/ourproject/kune/docs/client/cnt/DocumentContentPresenter.java
trunk/src/main/java/org/ourproject/kune/docs/client/cnt/reader/DocumentReader.java
trunk/src/main/java/org/ourproject/kune/docs/client/cnt/reader/DocumentReaderPresenter.java
trunk/src/main/java/org/ourproject/kune/platf/server/PlatformServerModule.java
trunk/src/main/java/org/ourproject/kune/platf/server/auth/AuthorizatedMethodInterceptor.java
trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileUploadManager.java
trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileUtils.java
trunk/src/main/java/org/ourproject/kune/platf/server/rpc/ContentRPC.java
trunk/src/main/resources/db/liquibase_changelog.xml
trunk/src/test/java/org/ourproject/kune/platf/server/manager/ContentManagerTest.java
trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/FileUploadManagerTest.java
trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/FileUtilsTest.java
Log:
liquibase migration, download servlet, content uploaded file show tests
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/pom.xml 2008-09-26 14:56:55 UTC (rev 886)
@@ -224,8 +224,13 @@
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
- <version>1.4.1</version>
+ <version>1.7.0</version>
</dependency>
+ <dependency>
+ <groupId>org.liquibase</groupId>
+ <artifactId>liquibase-plugin</artifactId>
+ <version>1.7.0.0</version>
+ </dependency>
<!-- jetty dependencies -->
<dependency>
@@ -424,7 +429,7 @@
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-plugin</artifactId>
- <version>1.4.1.0</version>
+ <version>1.7.0.0</version>
<executions>
<execution>
<phase>process-resources</phase>
@@ -447,7 +452,7 @@
</configuration>
-->
<goals>
- <goal>migrate</goal>
+ <goal>update</goal>
</goals>
</execution>
</executions>
Modified: trunk/src/main/java/org/ourproject/kune/app/server/KuneRackModule.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/app/server/KuneRackModule.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/app/server/KuneRackModule.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -28,6 +28,7 @@
import org.ourproject.kune.platf.client.rpc.SocialNetworkService;
import org.ourproject.kune.platf.server.LoggerMethodInterceptor;
import org.ourproject.kune.platf.server.PlatformServerModule;
+import org.ourproject.kune.platf.server.manager.impl.FileDownloadManager;
import org.ourproject.kune.platf.server.manager.impl.FileUploadManager;
import org.ourproject.kune.platf.server.properties.PropertiesFileName;
import org.ourproject.kune.platf.server.rest.ContentJSONService;
@@ -96,7 +97,7 @@
UserService.class, SocialNetworkService.class, I18nService.class);
builder.installRESTServices("^/kune/json/", TestJSONService.class, GroupJSONService.class,
UserJSONService.class, I18nTranslationJSONService.class, ContentJSONService.class);
- builder.installServlet("^/kune/servlets/", FileUploadManager.class);
+ builder.installServlet("^/kune/servlets/", FileUploadManager.class, FileDownloadManager.class);
builder.at("^/kune/(.*)$").install(new ForwardFilter("^/kune/(.*)$", "/gwt/org.ourproject.kune.app.Kune/{0}"));
}
Modified: trunk/src/main/java/org/ourproject/kune/docs/client/DocumentClientModule.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/docs/client/DocumentClientModule.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/docs/client/DocumentClientModule.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -173,7 +173,8 @@
register(Singleton.class, new Factory<DocumentReader>(DocumentReader.class) {
public DocumentReader create() {
final DocumentReaderView view = new DocumentReaderPanel($(WorkspaceSkeleton.class));
- final DocumentReaderPresenter presenter = new DocumentReaderPresenter(view);
+ final DocumentReaderPresenter presenter = new DocumentReaderPresenter($(Session.class), view,
+ $(I18nUITranslationService.class));
return presenter;
}
});
Modified: trunk/src/main/java/org/ourproject/kune/docs/client/cnt/DocumentContentPresenter.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/docs/client/cnt/DocumentContentPresenter.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/docs/client/cnt/DocumentContentPresenter.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -203,9 +203,9 @@
private void showContent() {
textEditorProvider.get().setToolbarVisible(false);
if (content.hasDocument()) {
- docReaderProvider.get().showDocument(content.getContent());
+ docReaderProvider.get().showDocument(content.getStateToken(), content.getContent(), content.getTypeId(),
+ content.getMimeType());
textEditorProvider.get().reset();
- docReaderProvider.get().show();
} else {
final FolderViewer viewer = folderViewerProvider.get();
viewer.setFolder(content.getContainer());
Modified: trunk/src/main/java/org/ourproject/kune/docs/client/cnt/reader/DocumentReader.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/docs/client/cnt/reader/DocumentReader.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/docs/client/cnt/reader/DocumentReader.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -21,12 +21,12 @@
package org.ourproject.kune.docs.client.cnt.reader;
import org.ourproject.kune.platf.client.View;
+import org.ourproject.kune.platf.client.dto.BasicMimeTypeDTO;
+import org.ourproject.kune.platf.client.dto.StateToken;
public interface DocumentReader {
View getView();
- void show();
-
- void showDocument(String text);
+ void showDocument(StateToken token, String text, String typeId, BasicMimeTypeDTO mimeType);
}
Modified: trunk/src/main/java/org/ourproject/kune/docs/client/cnt/reader/DocumentReaderPresenter.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/docs/client/cnt/reader/DocumentReaderPresenter.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/docs/client/cnt/reader/DocumentReaderPresenter.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -20,23 +20,47 @@
package org.ourproject.kune.docs.client.cnt.reader;
+import org.ourproject.kune.docs.client.DocumentClientTool;
import org.ourproject.kune.platf.client.View;
+import org.ourproject.kune.platf.client.dto.BasicMimeTypeDTO;
+import org.ourproject.kune.platf.client.dto.StateToken;
+import org.ourproject.kune.platf.client.state.Session;
+import org.ourproject.kune.workspace.client.i18n.I18nUITranslationService;
public class DocumentReaderPresenter implements DocumentReader {
private final DocumentReaderView view;
+ private final I18nUITranslationService i18n;
+ private final Session session;
- public DocumentReaderPresenter(final DocumentReaderView view) {
+ public DocumentReaderPresenter(final Session session, final DocumentReaderView view,
+ final I18nUITranslationService i18n) {
+ this.session = session;
this.view = view;
+ this.i18n = i18n;
}
public View getView() {
return view;
}
- public void show() {
+ public void showDocument(final StateToken token, final String text, final String typeId,
+ final BasicMimeTypeDTO mimeType) {
+ if (typeId.equals(DocumentClientTool.TYPE_UPLOADEDFILE)) {
+ if (mimeType != null) {
+ final String url = "/kune/servlets/FileDownloadManager?token=" + token + "&hash="
+ + session.getUserHash();
+ if (mimeType.getType().equals("image")) {
+ view.setContent("<img src=\"" + url + "\">");
+ } else if (mimeType.toString().equals("text/plain")) {
+ view.setContent(text);
+ } else {
+ view.setContent("<a href=\"" + url + "\">" + i18n.t("Download") + "</a>");
+ }
+ } else {
+ view.setContent("<a href=\"" + "dd" + "\">" + i18n.t("Download") + "</a>");
+ }
+ } else {
+ view.setContent(text);
+ }
}
-
- public void showDocument(final String text) {
- view.setContent(text);
- }
}
Modified: trunk/src/main/java/org/ourproject/kune/platf/server/PlatformServerModule.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/PlatformServerModule.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/PlatformServerModule.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -63,6 +63,7 @@
import org.ourproject.kune.platf.server.manager.TagManager;
import org.ourproject.kune.platf.server.manager.ToolConfigurationManager;
import org.ourproject.kune.platf.server.manager.UserManager;
+import org.ourproject.kune.platf.server.manager.impl.FileDownloadManager;
import org.ourproject.kune.platf.server.manager.impl.FileManagerDefault;
import org.ourproject.kune.platf.server.manager.impl.FileUploadManager;
import org.ourproject.kune.platf.server.manager.impl.GroupManagerDefault;
@@ -109,6 +110,7 @@
bind(Mapper.class).to(DozerMapper.class);
bind(ToolRegistry.class);
bind(FileUploadManager.class);
+ bind(FileDownloadManager.class);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(Authenticated.class),
outermostCall(new AuthenticatedMethodInterceptor()));
Modified: trunk/src/main/java/org/ourproject/kune/platf/server/auth/AuthorizatedMethodInterceptor.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/auth/AuthorizatedMethodInterceptor.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/auth/AuthorizatedMethodInterceptor.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -25,12 +25,12 @@
import org.aopalliance.intercept.MethodInvocation;
import org.ourproject.kune.platf.client.dto.StateToken;
import org.ourproject.kune.platf.client.errors.AccessViolationException;
-import org.ourproject.kune.platf.client.errors.ContentNotFoundException;
import org.ourproject.kune.platf.server.UserSession;
import org.ourproject.kune.platf.server.access.AccessRights;
import org.ourproject.kune.platf.server.access.AccessRightsService;
import org.ourproject.kune.platf.server.access.AccessRol;
import org.ourproject.kune.platf.server.access.AccessService;
+import org.ourproject.kune.platf.server.content.ContentUtils;
import org.ourproject.kune.platf.server.domain.Container;
import org.ourproject.kune.platf.server.domain.Content;
import org.ourproject.kune.platf.server.domain.Group;
@@ -76,18 +76,20 @@
switch (actionLevel) {
case content:
default:
- final Content content = accessService.accessToContent(parseId(token.getDocument()), user, accessRol);
+ final Content content = accessService.accessToContent(ContentUtils.parseId(token.getDocument()), user,
+ accessRol);
if (!content.getContainer().getOwner().equals(group)) {
throw new AccessViolationException();
}
- if (!content.getContainer().getId().equals(parseId(token.getFolder()))) {
+ if (!content.getContainer().getId().equals(ContentUtils.parseId(token.getFolder()))) {
throw new AccessViolationException();
}
if (!content.getContainer().getToolName().equals(token.getTool())) {
throw new AccessViolationException();
}
case container:
- final Container container = accessService.accessToContainer(parseId(token.getFolder()), user, accessRol);
+ final Container container = accessService.accessToContainer(ContentUtils.parseId(token.getFolder()), user,
+ accessRol);
if (!container.getOwner().equals(group)) {
throw new AccessViolationException();
}
@@ -119,12 +121,4 @@
}
}
- private Long parseId(final String documentId) throws ContentNotFoundException {
- try {
- return new Long(documentId);
- } catch (final NumberFormatException e) {
- throw new ContentNotFoundException();
- }
- }
-
}
Added: trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentUtils.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentUtils.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/content/ContentUtils.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -0,0 +1,14 @@
+package org.ourproject.kune.platf.server.content;
+
+import org.ourproject.kune.platf.client.errors.ContentNotFoundException;
+
+public class ContentUtils {
+
+ public static Long parseId(final String id) throws ContentNotFoundException {
+ try {
+ return new Long(id);
+ } catch (final NumberFormatException e) {
+ throw new ContentNotFoundException();
+ }
+ }
+}
Added: trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileDownloadManager.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileDownloadManager.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileDownloadManager.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -0,0 +1,105 @@
+package org.ourproject.kune.platf.server.manager.impl;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.ourproject.kune.platf.client.dto.StateToken;
+import org.ourproject.kune.platf.client.errors.ContentNotFoundException;
+import org.ourproject.kune.platf.server.access.AccessRol;
+import org.ourproject.kune.platf.server.auth.ActionLevel;
+import org.ourproject.kune.platf.server.auth.Authenticated;
+import org.ourproject.kune.platf.server.auth.Authorizated;
+import org.ourproject.kune.platf.server.content.ContentManager;
+import org.ourproject.kune.platf.server.content.ContentUtils;
+import org.ourproject.kune.platf.server.domain.BasicMimeType;
+import org.ourproject.kune.platf.server.domain.Content;
+import org.ourproject.kune.platf.server.properties.KuneProperties;
+
+import com.google.inject.Inject;
+
+/**
+ * Some snippets from:
+ * http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index1.html?page=1
+ *
+ */
+public class FileDownloadManager extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ @Inject
+ ContentManager contentManager;
+ @Inject
+ KuneProperties kuneProperties;
+
+ public void returnFile(final String filename, final OutputStream out) throws FileNotFoundException, IOException {
+ InputStream in = null;
+ try {
+ in = new BufferedInputStream(new FileInputStream(filename));
+ final byte[] buf = new byte[4 * 1024]; // 4K buffer
+ int bytesRead;
+ while ((bytesRead = in.read(buf)) != -1) {
+ out.write(buf, 0, bytesRead);
+ }
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ }
+ }
+
+ @Override
+ protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
+ IOException {
+
+ final String userHash = req.getParameter("hash");
+ final StateToken stateToken = new StateToken(req.getParameter("token"));
+ final Content cnt = getContentForDownload(userHash, stateToken);
+
+ final String absDir = kuneProperties.get(KuneProperties.UPLOAD_LOCATION) + FileUtils.toDir(stateToken);
+ final String filename = new String(cnt.getLastRevision().getBody());
+ final String absFilename = absDir + filename;
+
+ doBuildResp(resp, absFilename, cnt.getTitle(), cnt.getMimeType(), FileUtils
+ .getFileNameExtension(filename, true));
+ }
+
+ private void doBuildResp(final HttpServletResponse resp, final String filename, final String otherName,
+ final BasicMimeType mimeType, final String extension) throws FileNotFoundException, IOException {
+ final File file = new File(filename);
+
+ resp.setContentLength((int) file.length());
+ if (mimeType == null) {
+ resp.setContentType("application/x-download");
+ } else if (mimeType.getType().equals("image") || mimeType.toString().equals("text/plain")) {
+ resp.setContentType(mimeType.toString());
+ } else {
+ resp.setContentType("application/x-download");
+ }
+ resp.setHeader("Content-Disposition", "attachment; filename=\"" + otherName + extension + "\"");
+
+ final OutputStream out = resp.getOutputStream();
+ returnFile(filename, out);
+ }
+
+ @Authenticated(mandatory = false)
+ @Authorizated(accessRolRequired = AccessRol.Viewer, actionLevel = ActionLevel.content)
+ private Content getContentForDownload(final String userHash, final StateToken stateToken) throws ServletException {
+ try {
+ return contentManager.find(ContentUtils.parseId(stateToken.getDocument()));
+ } catch (final ContentNotFoundException e) {
+ // what response to send in this case ?
+ throw new ServletException();
+ }
+
+ }
+}
Modified: trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileUploadManager.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileUploadManager.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileUploadManager.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -3,9 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
import java.io.Writer;
-import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
@@ -37,6 +35,7 @@
import org.ourproject.kune.platf.server.auth.Authenticated;
import org.ourproject.kune.platf.server.auth.Authorizated;
import org.ourproject.kune.platf.server.content.ContentManager;
+import org.ourproject.kune.platf.server.content.ContentUtils;
import org.ourproject.kune.platf.server.domain.BasicMimeType;
import org.ourproject.kune.platf.server.domain.Container;
import org.ourproject.kune.platf.server.domain.Content;
@@ -54,8 +53,6 @@
public static final Log log = LogFactory.getLog(FileUploadManager.class);
- private static final String SLASH = File.separator;
-
private static final String UTF8 = "UTF-8";
private static final long serialVersionUID = 1L;
@@ -118,7 +115,7 @@
file = item;
}
}
- createFile(userHash, stateToken, fileName, file);
+ createUploadedFile(userHash, stateToken, fileName, file);
} catch (final FileUploadException e) {
jsonResponse = createJsonResponse(false, i18n.t("Error: File too large"));
} catch (final Exception e) {
@@ -136,47 +133,50 @@
@Authenticated
@Authorizated(accessRolRequired = AccessRol.Editor, actionLevel = ActionLevel.container)
@Transactional(type = TransactionType.READ_WRITE)
- Content createFile(final String userHash, final StateToken stateToken, final String fileName,
+ Content createUploadedFile(final String userHash, final StateToken stateToken, final String fileName,
final FileItem fileUploadItem) throws Exception {
- final String relDir = calculateUploadDirLocation(stateToken, false);
- final String relDirEncoded = calculateUploadDirLocation(stateToken, true);
+ final String relDir = FileUtils.toDir(stateToken);
final String absDir = kuneProperties.get(KuneProperties.UPLOAD_LOCATION) + relDir;
fileManager.mkdir(absDir);
- final String filenameUTF8 = new String(fileName.getBytes(), UTF8);
- final File file = fileManager.createFileWithSequentialName(absDir, filenameUTF8);
- fileUploadItem.write(file);
- // FIXME remove file if not success
- String mimetype = "unknown/unknown";
- String extension = "";
+ File file = null;
try {
- final MagicMatch magicMatch = fileManager.getMimeType(file);
- extension = magicMatch.getExtension();
- mimetype = magicMatch.getMimeType();
- } catch (final MagicParseException e) {
- log.info("Exception: " + e.getCause());
- } catch (final MagicMatchNotFoundException e) {
- log.info("Exception: " + e.getCause());
- } catch (final MagicException e) {
- log.info("Exception: " + e.getCause());
- }
+ final String filenameUTF8 = new String(fileName.getBytes(), UTF8);
+ file = fileManager.createFileWithSequentialName(absDir, filenameUTF8);
+ fileUploadItem.write(file);
- // Persist
- final User user = userSession.getUser();
- final Container container = accessService.accessToContainer(new Long(stateToken.getFolder()), user,
- AccessRol.Editor);
- final Content content = contentManager.createContent(FileUtils.getFileNameWithoutExtension(file.getName(),
- extension), relDirEncoded + SLASH + URLEncoder.encode(filenameUTF8, UTF8), user, container);
- content.setTypeId(DocumentServerTool.TYPE_UPLOADEDFILE);
- content.setMimeType(new BasicMimeType(mimetype));
- return content;
- }
+ log.info("fileItem mime type: " + fileUploadItem.getContentType());
- private String calculateUploadDirLocation(final StateToken stateToken, final boolean urlEncoded)
- throws UnsupportedEncodingException {
- final String group = stateToken.getGroup();
- return SLASH + (urlEncoded ? URLEncoder.encode(group, UTF8) : group) + SLASH + stateToken.getTool() + SLASH
- + stateToken.getFolder();
+ String mimetype = "unknown/unknown";
+ String extension = "";
+ try {
+ final MagicMatch magicMatch = fileManager.getMimeType(file);
+
+ extension = magicMatch.getExtension();
+ mimetype = magicMatch.getMimeType();
+ } catch (final MagicParseException e) {
+ log.info("Exception: " + e.getCause());
+ } catch (final MagicMatchNotFoundException e) {
+ log.info("Exception: " + e.getCause());
+ } catch (final MagicException e) {
+ log.info("Exception: " + e.getCause());
+ }
+
+ // Persist
+ final User user = userSession.getUser();
+ final Container container = accessService.accessToContainer(ContentUtils.parseId(stateToken.getFolder()),
+ user, AccessRol.Editor);
+ final Content content = contentManager.createContent(FileUtils.getFileNameWithoutExtension(file.getName(),
+ extension), filenameUTF8, user, container);
+ content.setTypeId(DocumentServerTool.TYPE_UPLOADEDFILE);
+ content.setMimeType(new BasicMimeType(mimetype));
+ return content;
+ } catch (final Exception e) {
+ if (file != null && file.exists()) {
+ file.delete();
+ }
+ throw e;
+ }
}
private JSONObject createJsonResponse(final boolean success, final String message) {
Modified: trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileUtils.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileUtils.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/manager/impl/FileUtils.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -1,7 +1,35 @@
package org.ourproject.kune.platf.server.manager.impl;
+import java.io.File;
+
+import org.ourproject.kune.platf.client.dto.StateToken;
+
public class FileUtils {
+ public static final String SLASH = File.separator;
+
+ /**
+ * For filename extension info see:
+ * http://en.wikipedia.org/wiki/File_name_extension
+ *
+ * @param fileName
+ * @return
+ */
+ public static String getFileNameExtension(final String fileName, final boolean withDot) {
+ // also we can use FilenameUtils
+ final int dotIndex = fileName.lastIndexOf(".");
+ if (dotIndex == -1 || dotIndex == 0) {
+ return "";
+ } else {
+ if (withDot) {
+ final String ext = fileName.substring(dotIndex);
+ return ext.length() == 1 ? "" : ext;
+ } else {
+ return fileName.substring(dotIndex + 1);
+ }
+ }
+ }
+
public static String getFileNameWithoutExtension(final String fileName, final String extension) {
final int extlength = extension.length();
if (extlength > 0) {
@@ -36,4 +64,8 @@
return getNextSequentialFileName(fileName.substring(0, dotIndex)) + ext;
}
}
+
+ public static String toDir(final StateToken stateToken) {
+ return SLASH + stateToken.getGroup() + SLASH + stateToken.getTool() + SLASH + stateToken.getFolder() + SLASH;
+ }
}
Modified: trunk/src/main/java/org/ourproject/kune/platf/server/rpc/ContentRPC.java
===================================================================
--- trunk/src/main/java/org/ourproject/kune/platf/server/rpc/ContentRPC.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/java/org/ourproject/kune/platf/server/rpc/ContentRPC.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -53,6 +53,7 @@
import org.ourproject.kune.platf.server.content.CommentManager;
import org.ourproject.kune.platf.server.content.ContainerManager;
import org.ourproject.kune.platf.server.content.ContentManager;
+import org.ourproject.kune.platf.server.content.ContentUtils;
import org.ourproject.kune.platf.server.content.CreationService;
import org.ourproject.kune.platf.server.domain.AccessLists;
import org.ourproject.kune.platf.server.domain.Comment;
@@ -116,7 +117,7 @@
@Transactional(type = TransactionType.READ_WRITE)
public void addAuthor(final String userHash, final StateToken token, final String authorShortName)
throws DefaultException {
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
contentManager.addAuthor(user, contentId, authorShortName);
@@ -129,7 +130,7 @@
final String commentText) throws DefaultException {
final UserSession userSession = getUserSession();
final User author = userSession.getUser();
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final Comment comment = commentManager.addComment(author, contentId, commentText, parentCommentId);
return mapper.map(comment, CommentDTO.class);
}
@@ -141,7 +142,7 @@
throws DefaultException {
final UserSession userSession = getUserSession();
final User author = userSession.getUser();
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final Comment comment = commentManager.addComment(author, contentId, commentText);
return mapper.map(comment, CommentDTO.class);
}
@@ -155,8 +156,8 @@
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
final boolean userIsLoggedIn = userSession.isUserLoggedIn();
- final Container container = accessService.accessToContainer(parseId(parentToken.getFolder()), user,
- AccessRol.Editor);
+ final Container container = accessService.accessToContainer(ContentUtils.parseId(parentToken.getFolder()),
+ user, AccessRol.Editor);
final Content addedContent = creationService.createContent(title, "", user, container);
final Access access = accessService.getAccess(user, addedContent.getStateToken(), group, AccessRol.Editor);
final State state = stateService.create(access);
@@ -173,7 +174,8 @@
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
final boolean userIsLoggedIn = userSession.isUserLoggedIn();
- final State state = createFolder(parentToken.getGroup(), parseId(parentToken.getFolder()), title, contentTypeId);
+ final State state = createFolder(parentToken.getGroup(), ContentUtils.parseId(parentToken.getFolder()), title,
+ contentTypeId);
completeState(user, userIsLoggedIn, group, state);
return mapState(state, user, group);
}
@@ -194,7 +196,7 @@
xmppManager.createRoom(connection, roomName, userShortName + userHash);
xmppManager.disconnect(connection);
try {
- final State state = createFolder(groupShortName, parseId(parentToken.getFolder()), roomName,
+ final State state = createFolder(groupShortName, ContentUtils.parseId(parentToken.getFolder()), roomName,
ChatServerTool.TYPE_ROOM);
completeState(user, userIsLoggedIn, group, state);
return mapState(state, user, group);
@@ -214,7 +216,7 @@
@Authorizated(accessRolRequired = AccessRol.Administrator)
@Transactional(type = TransactionType.READ_WRITE)
public void delContent(final String userHash, final StateToken token) throws DefaultException {
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
contentManager.delContent(user, contentId);
@@ -264,7 +266,7 @@
throws DefaultException {
final UserSession userSession = getUserSession();
final User informer = userSession.getUser();
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final Comment comment = commentManager.markAsAbuse(informer, contentId, commentId);
return mapper.map(comment, CommentDTO.class);
}
@@ -275,7 +277,7 @@
public void rateContent(final String userHash, final StateToken token, final Double value) throws DefaultException {
final UserSession userSession = getUserSession();
final User rater = userSession.getUser();
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
if (userSession.isUserLoggedIn()) {
contentManager.rateContent(rater, contentId, value);
@@ -289,7 +291,7 @@
@Transactional(type = TransactionType.READ_WRITE)
public void removeAuthor(final String userHash, final StateToken token, final String authorShortName)
throws DefaultException {
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
contentManager.removeAuthor(user, contentId, authorShortName);
@@ -300,7 +302,7 @@
@Transactional(type = TransactionType.READ_WRITE)
public String renameContainer(final String userHash, final StateToken token, final String newName)
throws DefaultException {
- return renameFolder(token.getGroup(), parseId(token.getFolder()), newName);
+ return renameFolder(token.getGroup(), ContentUtils.parseId(token.getFolder()), newName);
}
@Authenticated
@@ -311,7 +313,7 @@
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
try {
- accessService.accessToContent(parseId(token.getDocument()), user, AccessRol.Editor);
+ accessService.accessToContent(ContentUtils.parseId(token.getDocument()), user, AccessRol.Editor);
} catch (final NoResultException e) {
throw new AccessViolationException();
}
@@ -324,7 +326,7 @@
public Integer save(final String userHash, final StateToken token, final String textContent)
throws DefaultException {
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
final Content content = accessService.accessToContent(contentId, user, AccessRol.Editor);
@@ -336,7 +338,7 @@
@Authorizated(accessRolRequired = AccessRol.Administrator)
@Transactional(type = TransactionType.READ_WRITE)
public ContentDTO setAsDefaultContent(final String userHash, final StateToken token) {
- final Content content = contentManager.find(parseId(token.getDocument()));
+ final Content content = contentManager.find(ContentUtils.parseId(token.getDocument()));
groupManager.setDefaultContent(token.getGroup(), content);
return mapper.map(content, ContentDTO.class);
}
@@ -346,7 +348,7 @@
@Transactional(type = TransactionType.READ_WRITE)
public I18nLanguageDTO setLanguage(final String userHash, final StateToken token, final String languageCode)
throws DefaultException {
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
return mapper.map(contentManager.setLanguage(user, contentId, languageCode), I18nLanguageDTO.class);
@@ -357,7 +359,7 @@
@Transactional(type = TransactionType.READ_WRITE)
public void setPublishedOn(final String userHash, final StateToken token, final Date publishedOn)
throws DefaultException {
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
contentManager.setPublishedOn(user, contentId, publishedOn);
@@ -370,14 +372,14 @@
if (status.equals(ContentStatusDTO.publishedOnline) || status.equals(ContentStatusDTO.rejected)) {
throw new AccessViolationException();
}
- contentManager.setStatus(parseId(token.getDocument()), ContentStatus.valueOf(status.toString()));
+ contentManager.setStatus(ContentUtils.parseId(token.getDocument()), ContentStatus.valueOf(status.toString()));
}
@Authenticated
@Authorizated(accessRolRequired = AccessRol.Administrator)
@Transactional(type = TransactionType.READ_WRITE)
public void setStatusAsAdmin(final String userHash, final StateToken token, final ContentStatusDTO status) {
- contentManager.setStatus(parseId(token.getDocument()), ContentStatus.valueOf(status.toString()));
+ contentManager.setStatus(ContentUtils.parseId(token.getDocument()), ContentStatus.valueOf(status.toString()));
}
@Authenticated
@@ -385,7 +387,7 @@
@Transactional(type = TransactionType.READ_WRITE)
public List<TagResultDTO> setTags(final String userHash, final StateToken token, final String tags)
throws DefaultException {
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
final Group group = groupManager.findByShortName(token.getGroup());
@@ -400,14 +402,14 @@
final boolean votePositive) throws DefaultException {
final UserSession userSession = getUserSession();
final User voter = userSession.getUser();
- final Long contentId = parseId(token.getDocument());
+ final Long contentId = ContentUtils.parseId(token.getDocument());
final Comment comment = commentManager.vote(voter, contentId, commentId, votePositive);
return mapper.map(comment, CommentDTO.class);
}
private void completeState(final User user, final boolean userIsLoggedIn, final Group group, final State state) {
if (state.isRateable()) {
- final Long contentId = parseId(state.getDocumentId());
+ final Long contentId = ContentUtils.parseId(state.getDocumentId());
final Content content = contentManager.find(contentId);
if (userIsLoggedIn) {
state.setCurrentUserRate(contentManager.getRateContent(user, content));
@@ -462,17 +464,9 @@
return stateDTO;
}
- private Long parseId(final String id) throws ContentNotFoundException {
- try {
- return new Long(id);
- } catch (final NumberFormatException e) {
- throw new ContentNotFoundException();
- }
- }
-
private String renameContent(final String documentId, final String newName) throws ContentNotFoundException,
DefaultException {
- final Long contentId = parseId(documentId);
+ final Long contentId = ContentUtils.parseId(documentId);
final UserSession userSession = getUserSession();
final User user = userSession.getUser();
return contentManager.renameContent(user, contentId, newName);
Modified: trunk/src/main/resources/db/liquibase_changelog.xml
===================================================================
--- trunk/src/main/resources/db/liquibase_changelog.xml 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/main/resources/db/liquibase_changelog.xml 2008-09-26 14:56:55 UTC (rev 886)
@@ -1,8 +1,8 @@
<databaseChangeLog
- xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.4"
+ xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.4
- http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.4.xsd">
+ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7
+ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
<preConditions>
<!-- only execute this if the database exists (see DtabaInitializer.java -->
<sqlCheck expectedResult="1">
Modified: trunk/src/test/java/org/ourproject/kune/platf/server/manager/ContentManagerTest.java
===================================================================
--- trunk/src/test/java/org/ourproject/kune/platf/server/manager/ContentManagerTest.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/test/java/org/ourproject/kune/platf/server/manager/ContentManagerTest.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -45,6 +45,8 @@
private License defLicense;
public ContentManagerTest() {
+ // Testing with mysql because utf-8 normally fails with mysql and not in
+ // memory
super("test_db", "kune.properties");
}
@@ -86,6 +88,10 @@
createContentWithMime(mimetype);
}
+ /**
+ * This normally fails with mysql (not configured for utf-8), see the
+ * INSTALL mysql section
+ */
@Test
public void testUTF8Persist() {
final Container container = Mockito.mock(Container.class);
Modified: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/FileUploadManagerTest.java
===================================================================
--- trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/FileUploadManagerTest.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/FileUploadManagerTest.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -33,11 +33,11 @@
@Test(expected = SessionExpiredException.class)
public void testSessionExp() throws Exception {
- fileUploadManager.createFile("otherhash", null, null, null);
+ fileUploadManager.createUploadedFile("otherhash", null, null, null);
}
@Test(expected = UserMustBeLoggedException.class)
public void testUserMustBeAuth() throws Exception {
- fileUploadManager.createFile(null, null, null, null);
+ fileUploadManager.createUploadedFile(null, null, null, null);
}
}
Modified: trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/FileUtilsTest.java
===================================================================
--- trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/FileUtilsTest.java 2008-09-26 01:40:25 UTC (rev 885)
+++ trunk/src/test/java/org/ourproject/kune/platf/server/manager/impl/FileUtilsTest.java 2008-09-26 14:56:55 UTC (rev 886)
@@ -20,6 +20,36 @@
}
@Test
+ public void getExtensionBasic() {
+ assertEquals("someext", FileUtils.getFileNameExtension("file.name.with.someext", false));
+ }
+
+ @Test
+ public void getExtensionWithDot() {
+ assertEquals("", FileUtils.getFileNameExtension("file name with no ext.", false));
+ }
+
+ @Test
+ public void getExtensionWithDotBasic() {
+ assertEquals(".someext", FileUtils.getFileNameExtension("file.name.with.someext", true));
+ }
+
+ @Test
+ public void getExtensionWithDotWithFinalDot() {
+ assertEquals("", FileUtils.getFileNameExtension("file name with no ext.", true));
+ }
+
+ @Test
+ public void getExtensionWithDotWithInitialDot() {
+ assertEquals("", FileUtils.getFileNameExtension(".file name with no ext", true));
+ }
+
+ @Test
+ public void getExtensionWithInitialDot() {
+ assertEquals("", FileUtils.getFileNameExtension(".file name with no ext", false));
+ }
+
+ @Test
public void testFileNameWExtensionSimple() {
assertEquals("test", FileUtils.getFileNameWithoutExtension("test.txt", "txt"));
}
More information about the kune-commits
mailing list