idok-commit AT lists.psi.ch
Subject: Commit emails of the iDok project
List archive
- From: "AFS account Florian Huebner" <huebner AT savannah.psi.ch>
- To: idok-commit AT lists.psi.ch
- Subject: [idok-commit] idok commit r384 - trunk/java/ch/idok/service/server/io/rest
- Date: Wed, 30 Sep 2009 14:26:13 +0200
- List-archive: <https://lists.web.psi.ch/pipermail/idok-commit/>
- List-id: Commit emails of the iDok project <idok-commit.lists.psi.ch>
Author: huebner
Date: Wed Sep 30 14:26:13 2009
New Revision: 384
Log:
Major overhaul for uploading files
Modified:
trunk/java/ch/idok/service/server/io/rest/RestIOServiceResource.java
Modified: trunk/java/ch/idok/service/server/io/rest/RestIOServiceResource.java
==============================================================================
--- trunk/java/ch/idok/service/server/io/rest/RestIOServiceResource.java
(original)
+++ trunk/java/ch/idok/service/server/io/rest/RestIOServiceResource.java
Wed Sep 30 14:26:13 2009
@@ -1,32 +1,61 @@
package ch.idok.service.server.io.rest;
+
+import java.io.File;
import java.net.URI;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.restlet.data.MediaType;
+import org.restlet.ext.fileupload.RestletFileUpload;
+import org.restlet.resource.FileRepresentation;
+import org.restlet.resource.Representation;
+
+import ch.idok.cli.command.DeleteCommand;
+import ch.idok.cli.command.PutCommand;
import ch.idok.common.client.ClientHandler;
import ch.idok.common.client.RESTClientHandler;
import ch.idok.common.errorhandling.DmsException;
+import ch.idok.common.errorhandling.ErrorType;
+import ch.idok.common.impl.repository.svn.SvnDocumentId;
import ch.idok.common.impl.repository.svn.SvnRepositoryManager;
import ch.idok.common.repository.DocumentId;
+import ch.idok.common.repository.DocumentVersion;
import ch.idok.common.repository.PathType;
import ch.idok.common.repository.Repository;
import ch.idok.common.repository.RepositoryDirEntry;
import ch.idok.common.repository.RepositoryId;
import ch.idok.common.repository.RepositoryManager;
import ch.idok.common.util.URIUtil;
+import ch.idok.service.server.admin.Admin;
import ch.idok.service.server.rest.RestExceptionResponse;
import ch.idok.service.server.rest.RestServer;
+
+
+
+
+
/*
* Copyright (C) 2006-2008 iDok team.
*
@@ -89,58 +118,210 @@
logger = server.getLogger();
baseURI = server.getBaseURI();
}
+
+
+ /**
+ * Mandatory. Specifies that this resource supports POST requests.
+ */
+ public boolean allowPost() {
+ return true;
+ }
+
+ /**
+ * Mandatory. Specifies that this resource supports POST requests.
+ */
+// public boolean allowPut() {
+// return true;
+// }
+
+
+ @GET
+ @Path("{project}/{repository}/")
+ @Produces("text/html")
+ public Response queryRootContent(
+ @PathParam("project") String project,
+ @PathParam("repository") String repository) {
+ logger.entering(this.getClass().getName(),
"queryFolderContent");
+
+ ClientHandler handler = new RESTClientHandler();
+ RepositoryManager mgr = new SvnRepositoryManager(handler);
+
+ try {
+
+ Admin.validateName(project);
+ Admin.validateName(repository);
+ String baseWithoutApi = baseURI.split("/api/v1")[0];
+
+ URI uri = URIUtil.parseUserURI(baseWithoutApi + "/" + project +
"/" + repository);
+
+ RepositoryId repoId = mgr.getRepositoryId(uri,
+ RepositoryManager.LocationType.ROOT);
+ Repository repo = mgr.getRepository(repoId);
+ DocumentId destId = repo.getDocumentId("");
+
+ StringBuilder sb;
+ if (repo.getPathType(destId) == PathType.DIRECTORY) {
+
+ Iterable<RepositoryDirEntry> entries;
+ entries =
repo.getDir(repo.getRepositoryPath(destId));
+
+ sb = new StringBuilder();
+ sb.append("<?xml version=\"1.0\"?> <!DOCTYPE
html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"> <html
xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">");
+ sb.append("<html><head><title>Revision"+
repo.getLatestVersion() + ": /</title></head>");
+ sb.append("<body>");
+ sb.append(" <h2>Revision " +
repo.getLatestVersion()+ ": /</h2>");
+ sb.append(" <ul>");
+ for (RepositoryDirEntry entry : entries) {
+ if (entry.getType() ==
PathType.DIRECTORY) {
+ // fillFolderRow(parent,
entry.getId().getTail());
+ // sb.append...
+ sb.append(String.format("
<li><a href=\"%s\">%s/</a></li>",
+
entry.getId(), entry.getId()));
+ } else if (entry.getType() ==
PathType.FILE) {
+ // fillDocumentRow(entry);
+ sb.append(String.format("
<li><a href=\"%s\">%s</a></li>",
+
uri.toString()+ "/" + entry.getId(), entry.getId()));
+ }
+ }
+ sb.append("</ul>");
+ sb.append("<hr noshade>");
+ sb.append("</body></html>");
+ return Response.ok().entity(sb).build();
+
+ }
+ else{
+ return
Response.status(Response.Status.BAD_REQUEST).build();
+ }
+
+ } catch (DmsException e) {
+ // 404 if the version doesnt exist
+ e.printStackTrace();
+ return Response.status(Response.Status.NOT_FOUND).build();
+
+ } catch (Exception e) {
+ // TODO: handle exception
+ logger.log(Level.FINER, "Error in REST call", e);
+ return RestExceptionResponse.generateXHTML(e);
+ }
+ }
+
+
+
/**
* Resource method for getting folder contents
*/
@GET
- @Path(value = "{path:.+}")
+ @Path("{project}/{repository}/{path:.+}")
@Produces("text/html")
- public Response queryFolderContent(@PathParam("path") String path) {
+ public Response queryFolderContent(
+ @PathParam("project") String project,
+ @PathParam("repository") String repository,
+ @PathParam("path") String path,
+ @QueryParam("version") String version) {
logger.entering(this.getClass().getName(),
"queryFolderContent");
- // Parse project, repository and folder
- String project = "";
- String repository = "";
- String folder = "";
-
- String[] sa = path.split("/", 3);
- project = sa[0];
- if (sa.length > 1)
- repository = sa[1];
- if (sa.length > 2)
- folder = sa[2];
+
ClientHandler handler = new RESTClientHandler();
RepositoryManager mgr = new SvnRepositoryManager(handler);
+
try {
// todo: create URI from path - is there a more
direct way in
// defining the path to the document ?
- URI uri =
URIUtil.parseUserURI("https://dms02.psi.ch/"+project+"/"+repository);
+
+
//https://dms.psi.ch/wiki/PSIWeb/!svn/ver/243/Clienten/ASI_Intranet/090616BilderWS/_PSI0014.JPG
+
+
+ Admin.validateName(project);
+ Admin.validateName(repository);
+ String baseWithoutApi = baseURI.split("/api/v1")[0];
+
+ String file = path.split("/")[path.split("/").length-1];
+ String dir="";
+// URI createdUri = null;
+// if (path.split("/").length > 1) {
+// dir=path.substring(0, path.length()-file.length()-1);
+// createdUri = new URI(baseURI +"/"+ project +"/"+ repository
+"/"+ dir + "/" + file);
+// }else{
+// dir ="";
+// createdUri = new URI(baseURI +"/"+ project +"/"+ repository +
"/" + file);
+// }
+
+
+ URI uri;
+// if (version != null) {
+// uri = URIUtil.parseUserURI(baseWithoutApi +
"/!svn/ver/"+ version + "/" + project + "/" + repository);
+// }else{
+ uri = URIUtil.parseUserURI(baseWithoutApi +
"/" + project + "/" + repository);
+// }
+
RepositoryId repoId = mgr.getRepositoryId(uri,
- RepositoryManager.LocationType.ROOT);
+ RepositoryManager.LocationType.ROOT);
Repository repo = mgr.getRepository(repoId);
- DocumentId docId = repo.getDocumentId(folder);
- Iterable<RepositoryDirEntry> entries =
repo.getDir(repo
- .getRepositoryPath(docId));
- StringBuilder sb = new StringBuilder();
- sb.append("<?xml version=\"1.0\"?> <!DOCTYPE html
PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"> <html
xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">");
- sb.append("<html><head><title>Revision" +
repo.getLatestVersion() + ": /</title></head>");
- sb.append("<body>");
- sb.append(" <h2>Revision " + repo.getLatestVersion()
+ ": /</h2>");
- sb.append(" <ul>");
- for (RepositoryDirEntry entry : entries) {
- if (entry.getType() == PathType.DIRECTORY) {
- // fillFolderRow(parent,
entry.getId().getTail());
- // sb.append...
- sb.append(String.format(" <li><a
href=\"%s\">%s/</a></li>",entry.getId(),entry.getId()));
- } else if (entry.getType() == PathType.FILE) {
- // fillDocumentRow(entry);
- sb.append(String.format(" <li><a
href=\"%s\">%s</a></li>",uri.toString()+"/"+entry.getId(),entry.getId()));
- }
- }
- sb.append("</ul>");
- sb.append("<hr noshade>");
- sb.append("</body></html>");
- return Response.ok().entity(sb).build();
+ DocumentId destId = repo.getDocumentId(path);
+
+ StringBuilder sb;
+ if (repo.getPathType(destId) == PathType.DIRECTORY) {
+
+ Iterable<RepositoryDirEntry> entries;
+ entries =
repo.getDir(repo.getRepositoryPath(destId));
+
+ sb = new StringBuilder();
+ sb.append("<?xml version=\"1.0\"?> <!DOCTYPE
html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"> <html
xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">");
+ sb.append("<html><head><title>Revision"+
repo.getLatestVersion() + ": /</title></head>");
+ sb.append("<body>");
+ sb.append(" <h2>Revision " +
repo.getLatestVersion()+ ": /</h2>");
+ sb.append(" <ul>");
+ for (RepositoryDirEntry entry : entries) {
+ if (entry.getType() ==
PathType.DIRECTORY) {
+ // fillFolderRow(parent,
entry.getId().getTail());
+ // sb.append...
+ sb.append(String.format("
<li><a href=\"%s\">%s/</a></li>",
+
entry.getId(), entry.getId()));
+ } else if (entry.getType() ==
PathType.FILE) {
+ // fillDocumentRow(entry);
+ sb.append(String.format("
<li><a href=\"%s\">%s</a></li>",
+
uri.toString()+ "/" + entry.getId(), entry.getId()));
+ }
+ }
+ sb.append("</ul>");
+ sb.append("<hr noshade>");
+ sb.append("</body></html>");
+ return Response.ok().entity(sb).build();
+
+ }else if (repo.getPathType(destId) == PathType.FILE) {
+ Map<String,String> metaMap = new
HashMap<String, String>();
+//
repo.getDocumentData(repo.getRepositoryPath(destId), (String)null, metaMap);
+ sb = new StringBuilder();
+ sb.append("<?xml version=\"1.0\"?> <!DOCTYPE
html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"> <html
xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">");
+ if (version != null) {
+
repo.getDocumentData(repo.getRepositoryPath(destId,repo.getDocumentVersion(version)),
(String)null, metaMap);
+ sb.append("<html><head><title>" +
file + " ("+ version + ")</title></head>");
+ sb.append("<body>");
+ sb.append(" <h2>" + file + "
(Revision : " + metaMap.get("svn:entry:committed-rev")+ "): /</h2>");
+ }else{
+
repo.getDocumentData(repo.getRepositoryPath(destId), (String)null, metaMap);
+ sb.append("<html><head><title>" +
file + "</title></head>");
+ sb.append("<body>");
+ sb.append(" <h2>" + file + ":
/</h2>");
+ }
+ sb.append(" <ul>");
+ for (Map.Entry<String,String> mapEntry :
metaMap.entrySet()) {
+ sb.append(String.format("<li>" +
mapEntry.getKey()+" : "+ mapEntry.getValue() + "</li>"));
+ }
+ sb.append("</ul>");
+ sb.append("<hr noshade>");
+ sb.append("</body></html>");
+ return Response.ok().entity(sb).build();
+ }
+ else{
+ return
Response.status(Response.Status.BAD_REQUEST).build();
+ }
+
+ } catch (DmsException e) {
+ // 404 if the version doesnt exist
+ e.printStackTrace();
+ return Response.status(Response.Status.NOT_FOUND).build();
+
} catch (Exception e) {
// TODO: handle exception
logger.log(Level.FINER, "Error in REST call", e);
@@ -149,4 +330,499 @@
}
// Use Formencoding as a result format for meta data (p.151 Restful
Web
// Services
+
+
+ @GET
+ @Path("{project}/{repository}/")
+ @Produces("text/plain")
+ public Response queryRootContentPlain(
+ @PathParam("project") String project,
+ @PathParam("repository") String repository) {
+ logger.entering(this.getClass().getName(),
"queryFolderContent");
+
+ ClientHandler handler = new RESTClientHandler();
+ RepositoryManager mgr = new SvnRepositoryManager(handler);
+
+ try {
+
+ Admin.validateName(project);
+ Admin.validateName(repository);
+ String baseWithoutApi = baseURI.split("/api/v1")[0];
+
+ URI uri = URIUtil.parseUserURI(baseWithoutApi + "/" + project +
"/" + repository);
+
+
+ RepositoryId repoId = mgr.getRepositoryId(uri,
+ RepositoryManager.LocationType.ROOT);
+ Repository repo = mgr.getRepository(repoId);
+ DocumentId destId = repo.getDocumentId("");
+
+ StringBuilder sb;
+ if (repo.getPathType(destId) == PathType.DIRECTORY) {
+
+ Iterable<RepositoryDirEntry> entries;
+ entries =
repo.getDir(repo.getRepositoryPath(destId));
+
+ sb = new StringBuilder();
+ for (RepositoryDirEntry entry : entries) {
+ if (entry.getType() ==
PathType.DIRECTORY) {
+ // fillFolderRow(parent,
entry.getId().getTail());
+ // sb.append...
+
sb.append(String.format("%s/",
+
entry.getId()));
+ } else if (entry.getType() ==
PathType.FILE) {
+ // fillDocumentRow(entry);
+ sb.append(String.format("%s",
+
entry.getId()));
+ }
+ }
+ return Response.ok().entity(sb).build();
+
+ }
+ else{
+ return
Response.status(Response.Status.BAD_REQUEST).build();
+ }
+
+ } catch (DmsException e) {
+ // 404 if the version doesnt exist
+ e.printStackTrace();
+ return Response.status(Response.Status.NOT_FOUND).build();
+
+ } catch (Exception e) {
+ // TODO: handle exception
+ logger.log(Level.FINER, "Error in REST call", e);
+ return RestExceptionResponse.generateXHTML(e);
+ }
+ }
+
+
+
+ /**
+ * Resource method for getting folder contents
+ */
+ @GET
+ @Path("{project}/{repository}/{path:.+}")
+ @Produces("text/plain")
+ public Response queryFolderContentPlain(
+ @PathParam("project") String project,
+ @PathParam("repository") String repository,
+ @PathParam("path") String path,
+ @QueryParam("version") String version) {
+ logger.entering(this.getClass().getName(),
"queryFolderContent");
+
+ ClientHandler handler = new RESTClientHandler();
+ RepositoryManager mgr = new SvnRepositoryManager(handler);
+
+ try {
+ // todo: create URI from path - is there a more
direct way in
+ // defining the path to the document ?
+
+
//https://dms.psi.ch/wiki/PSIWeb/!svn/ver/243/Clienten/ASI_Intranet/090616BilderWS/_PSI0014.JPG
+
+
+ Admin.validateName(project);
+ Admin.validateName(repository);
+ String baseWithoutApi = baseURI.split("/api/v1")[0];
+
+ String file = path.split("/")[path.split("/").length-1];
+ String dir="";
+// URI createdUri = null;
+// if (path.split("/").length > 1) {
+// dir=path.substring(0, path.length()-file.length()-1);
+// createdUri = new URI(baseURI +"/"+ project +"/"+ repository
+"/"+ dir + "/" + file);
+// }else{
+// dir ="";
+// createdUri = new URI(baseURI +"/"+ project +"/"+ repository +
"/" + file);
+// }
+
+
+ URI uri;
+// if (version != null) {
+// uri = URIUtil.parseUserURI(baseWithoutApi +
"/!svn/ver/"+ version + "/" + project + "/" + repository);
+// }else{
+ uri = URIUtil.parseUserURI(baseWithoutApi +
"/" + project + "/" + repository);
+// }
+
+ RepositoryId repoId = mgr.getRepositoryId(uri,
+ RepositoryManager.LocationType.ROOT);
+ Repository repo = mgr.getRepository(repoId);
+ DocumentId destId = repo.getDocumentId(path);
+
+ StringBuilder sb;
+ if (repo.getPathType(destId) == PathType.DIRECTORY) {
+
+ Iterable<RepositoryDirEntry> entries;
+ entries =
repo.getDir(repo.getRepositoryPath(destId));
+
+ sb = new StringBuilder();
+ for (RepositoryDirEntry entry : entries) {
+ if (entry.getType() ==
PathType.DIRECTORY) {
+ // fillFolderRow(parent,
entry.getId().getTail());
+ // sb.append...
+
sb.append(String.format("%s/",
+
entry.getId()));
+ } else if (entry.getType() ==
PathType.FILE) {
+ // fillDocumentRow(entry);
+ sb.append(String.format("%s",
+
entry.getId()));
+ }
+ }
+ return Response.ok().entity(sb).build();
+
+ }else if (repo.getPathType(destId) == PathType.FILE) {
+ Map<String,String> metaMap = new
HashMap<String, String>();
+//
repo.getDocumentData(repo.getRepositoryPath(destId), (String)null, metaMap);
+ sb = new StringBuilder();
+ if (version != null) {
+
repo.getDocumentData(repo.getRepositoryPath(destId,repo.getDocumentVersion(version)),
(String)null, metaMap);
+ sb.append("" + file + " (Revision : "
+ metaMap.get("svn:entry:committed-rev")+ ")");
+ }else{
+
repo.getDocumentData(repo.getRepositoryPath(destId), (String)null, metaMap);
+ sb.append("" + file + "");
+ }
+ for (Map.Entry<String,String> mapEntry :
metaMap.entrySet()) {
+ sb.append(String.format("" +
mapEntry.getKey()+" : "+ mapEntry.getValue() + ""));
+ }
+ return Response.ok().entity(sb).build();
+ }
+ else{
+ return
Response.status(Response.Status.BAD_REQUEST).build();
+ }
+
+ } catch (DmsException e) {
+ // 404 if the version doesnt exist
+ e.printStackTrace();
+ return Response.status(Response.Status.NOT_FOUND).build();
+
+ } catch (Exception e) {
+ // TODO: handle exception
+ logger.log(Level.FINER, "Error in REST call", e);
+ return RestExceptionResponse.generateXHTML(e);
+ }
+ }
+ // Use Formencoding as a result format for meta data (p.151 Restful
Web
+ // Services
+
+
+
+ @POST
+ @Path("{project}/{repository}/{path:.+}")
+ @Consumes("multipart/form-data")
+ public Response postData(Representation entity,
+ @PathParam("project") String project,
+ @PathParam("repository") String repository,
+ @PathParam("path") String path,
+ @QueryParam("meta") String metaString)
+ {
+ try {
+ logger.entering(this.getClass().getName(), "postData",
+ new Object[] { project, repository, path });
+
+
+ boolean document = false;
+ boolean metadata = false;
+ boolean modified = false;
+
+ Admin.validateName(project);
+ Admin.validateName(repository);
+ String baseWithoutApi = baseURI.split("/api/v1")[0];
+
+
+ String file = path.split("/")[path.split("/").length-1];
+ String dir="";
+ URI createdUri = null;
+ if (path.split("/").length > 1) {
+ dir=path.substring(0, path.length()-file.length()-1);
+ createdUri = new URI(baseURI +"/"+ project +"/"+ repository
+"/"+ dir + "/" + file);
+ }else{
+ dir ="";
+ createdUri = new URI(baseURI +"/"+ project +"/"+ repository +
"/" + file);
+ }
+
+
+ if (entity != null) {
+ if
(MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(),
+ true)) {
+
+ // The Apache FileUpload project parses HTTP requests
which
+ // conform to RFC 1867, "Form-based File Upload in
HTML". That
+ // is, if an HTTP request is submitted using the POST
method,
+ // and with a content type of "multipart/form-data", then
+ // FileUpload can parse that request, and get all
uploaded files
+ // as FileItem.
+
+ // 1/ Create a factory for disk-based file items
+ DiskFileItemFactory factory = new DiskFileItemFactory();
+ factory.setSizeThreshold(1000240);
+
+ // 2/ Create a new file upload handler based on the
Restlet
+ // FileUpload extension that will parse Restlet requests
and
+ // generates FileItems.
+ RestletFileUpload upload = new
RestletFileUpload(factory);
+ List items;
+ Map<String,String> meta = new HashMap<String, String>();
+ try {
+ ClientHandler handler = new
RESTClientHandler();
+ RepositoryManager mgr = new
SvnRepositoryManager(handler);
+ PutCommand put = new PutCommand(handler);
+ URI uri = URIUtil.parseUserURI(baseWithoutApi
+ "/" + project + "/" + repository);
+
+ Repository repo = null;
+ Repository.Transaction transaction;
+
+
+ RepositoryId repoId =
mgr.getRepositoryId(uri,RepositoryManager.LocationType.ROOT);
+ repo = mgr.getRepository(repoId);
+ transaction = repo.startTransaction();
+
+ DocumentId destId = repo.getDocumentId(path);
+
+
+ // 3/ Request is parsed by the handler which
generates a
+ // list of FileItems
+ items = upload.parseRepresentation(entity);
+
+
+ File theFile = null;
+ for (final Iterator it = items.iterator();
it.hasNext();) {
+ FileItem fi = (FileItem)it.next();
+
+ if (fi.isFormField()) { // Is a simple form field
+ metadata = true;
+ meta.put(fi.getFieldName(), fi.getString());
+
+ }else { // Is an uploaded file
+ document = true;
+ theFile =
File.createTempFile("tempDocument", "");
+ fi.write(theFile);
+
+
+
+
+ }
+ }
+
+
+ if (repo.getPathType(destId) == PathType.FILE) {
+ modified = true;
+ if (metadata &&
document) {
+
transaction.updateDocument(destId, theFile
+
.getAbsolutePath(), meta);
+ } else if (!metadata
&& document) {
+
transaction.updateDocument(destId, theFile
+
.getAbsolutePath(), null);
+ } else if (metadata
&& !document) {
+
transaction.updateDocument(destId, null, meta);
+ } else {
+ return
Response.status(
+
Response.Status.NOT_ACCEPTABLE).build();
+ }
+ }else{
+ if (metadata &&
document) {
+
transaction.addDocument(destId, theFile
+
.getAbsolutePath(), meta);
+ } else if (!metadata
&& document) {
+
transaction.addDocument(destId, theFile
+
.getAbsolutePath(), null);
+ } else if (metadata
&& !document) {
+
transaction.addDocument(destId, null, meta);
+ } else {
+ return
Response.status(
+
Response.Status.NOT_ACCEPTABLE).build();
+ }
+ }
+
+ transaction.end("");
+
+
+ if (theFile != null &&
theFile.exists()) {
+ theFile.delete();
+ }
+
+ } catch (DmsException e) {
+ // The message of all thrown exception is sent back
to
+ // client as simple plain text
+ e.printStackTrace();
+ return
Response.status(Response.Status.CONFLICT).build();
+
+ } catch (Exception e) {
+ // The message of all thrown exception is sent back
to
+ // client as simple plain text
+ e.printStackTrace();
+ return
Response.status(Response.Status.BAD_REQUEST).build();
+
+ }
+ }
+ } else {
+ // POST request with no entity.
Status.CLIENT_ERROR_BAD_REQUEST
+ return Response.status(Response.Status.BAD_REQUEST).build();
+ }
+
+
+ if (modified) {
+ return Response.status(Response.Status.OK).build();
+ }else{
+ return Response.created(createdUri).build();
+ }
+
+
+ } catch (Exception e) {
+ logger.log(Level.FINER, "Error in REST call", e);
+ return RestExceptionResponse.generateXHTML(e);
+ }
+
+ }
+
+
+
+ @DELETE
+ @Path("{project}/{repository}/{path:.+}")
+ public Response deleteData(Representation entity,
+ @PathParam("project") String project,
+ @PathParam("repository") String repository,
+ @PathParam("path") String path)
+ {
+ try {
+ logger.entering(this.getClass().getName(), "DeleteData",
+ new Object[] { project, repository, path });
+
+
+ boolean document = false;
+ boolean metadata = false;
+ boolean modified = false;
+
+ Admin.validateName(project);
+ Admin.validateName(repository);
+ String baseWithoutApi = baseURI.split("/api/v1")[0];
+
+
+ String file = path.split("/")[path.split("/").length-1];
+ String dir="";
+ URI createdUri = null;
+ if (path.split("/").length > 1) {
+ dir=path.substring(0, path.length()-file.length()-1);
+ createdUri = new URI(baseURI +"/"+ project +"/"+ repository
+"/"+ dir + "/" + file);
+ }else{
+ dir ="";
+ createdUri = new URI(baseURI +"/"+ project +"/"+ repository +
"/" + file);
+ }
+
+
+ try {
+ ClientHandler handler = new RESTClientHandler();
+ RepositoryManager mgr = new
SvnRepositoryManager(handler);
+ URI uri = URIUtil.parseUserURI(baseWithoutApi + "/" +
project + "/" + repository);
+
+ Repository repo = null;
+ Repository.Transaction transaction;
+
+
+ RepositoryId repoId =
mgr.getRepositoryId(uri,RepositoryManager.LocationType.ROOT);
+ repo = mgr.getRepository(repoId);
+ transaction = repo.startTransaction();
+
+ DocumentId destId = repo.getDocumentId(path);
+
+ if ((repo.getPathType(destId) ==
PathType.FILE)||(repo.getPathType(destId) == PathType.DIRECTORY)) {
+ transaction.deleteEntry(destId);
+ }else{
+ return
Response.status(Response.Status.NOT_FOUND).build();
+ }
+
+
+ transaction.end("");
+
+ return Response.status(Response.Status.OK).build();
+
+
+ } catch (Exception e) {
+ // The message of all thrown exception is sent back to
+ // client as simple plain text
+ e.printStackTrace();
+ return Response.status(Response.Status.BAD_REQUEST).build();
+
+ }
+ } catch (Exception e) {
+ logger.log(Level.FINER, "Error in REST call", e);
+ return RestExceptionResponse.generateXHTML(e);
+ }
+
+ }
+
+
+ @PUT
+// @Consumes("application/x-www-form-urlencoded")
+ @Path("{project}/{repository}/{path:.+}")
+ public Response putPath(Representation entity,
+ @PathParam("project") String project,
+ @PathParam("repository") String repository,
+ @PathParam("path") String path)
+ {
+ try {
+ logger.entering(this.getClass().getName(), "DeleteData",
+ new Object[] { project, repository, path });
+
+
+ boolean document = false;
+ boolean metadata = false;
+ boolean modified = false;
+
+ Admin.validateName(project);
+ Admin.validateName(repository);
+ String baseWithoutApi = baseURI.split("/api/v1")[0];
+
+
+ String file = path.split("/")[path.split("/").length-1];
+ String dir="";
+ URI createdUri = null;
+ if (path.split("/").length > 1) {
+ dir=path.substring(0, path.length()-file.length()-1);
+ createdUri = new URI(baseURI +"/"+ project +"/"+ repository
+"/"+ dir + "/" + file);
+ }else{
+ dir ="";
+ createdUri = new URI(baseURI +"/"+ project +"/"+ repository +
"/" + file);
+ }
+
+
+ try {
+ ClientHandler handler = new RESTClientHandler();
+ RepositoryManager mgr = new
SvnRepositoryManager(handler);
+ URI uri = URIUtil.parseUserURI(baseWithoutApi + "/" +
project + "/" + repository);
+
+ Repository repo = null;
+ Repository.Transaction transaction;
+
+
+ RepositoryId repoId =
mgr.getRepositoryId(uri,RepositoryManager.LocationType.ROOT);
+ repo = mgr.getRepository(repoId);
+ transaction = repo.startTransaction();
+
+ DocumentId destId = repo.getDocumentId(path);
+
+ if (repo.getPathType(destId) == PathType.NONE) {
+ transaction.createFolder(destId);
+ }else{
+ return
Response.status(Response.Status.CONFLICT).build();
+ }
+
+
+ transaction.end("");
+
+ return Response.created(createdUri).build();
+
+
+ } catch (Exception e) {
+ // The message of all thrown exception is sent back to
+ // client as simple plain text
+ e.printStackTrace();
+ return Response.status(Response.Status.BAD_REQUEST).build();
+
+ }
+ } catch (Exception e) {
+ logger.log(Level.FINER, "Error in REST call", e);
+ return RestExceptionResponse.generateXHTML(e);
+ }
+
+ }
+
}
- [idok-commit] idok commit r384 - trunk/java/ch/idok/service/server/io/rest, AFS account Florian Huebner, 09/30/2009
Archive powered by MHonArc 2.6.19.