From: <ta...@go...> - 2011-09-23 14:14:35
|
Revision: 14114 Author: don...@ma... Date: Fri Sep 23 07:12:57 2011 Log: Tidy up, refactor REST handlers into their own package (on grounds of being very boring). http://code.google.com/p/taverna/source/detail?r=14114 Added: /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/AccessDeniedHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/BadInputPortNameHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/BadPropertyValueHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/BadStateChangeHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/FileMessageHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/FileSegmentHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/FilesystemAccessHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/GeneralFailureHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/HandlerCore.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/IllegalArgumentHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/ImplementationProblemHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/InputStreamMessageHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/InvalidCredentialHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/JAXBExceptionHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoCreateHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoCredentialHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoDestroyHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoDirectoryEntryHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoListenerHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoUpdateHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NotOwnerHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/PermissionHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/T2FlowDocumentHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/UnknownRunHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/package-info.java Deleted: /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/AccessDeniedHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/BadInputPortNameHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/BadPropertyValueHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/BadStateChangeHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/FileMessageHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/FileSegmentHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/FilesystemAccessHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/GeneralFailureHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/HandlerCore.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/IllegalArgumentHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/ImplementationProblemHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/InputStreamMessageHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/InvalidCredentialHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/JAXBExceptionHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/NoCreateHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/NoCredentialHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/NoDestroyHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/NoDirectoryEntryHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/NoListenerHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/NoUpdateHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/NotOwnerHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/PermissionHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/T2FlowDocumentHandler.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/UnknownRunHandler.java Modified: /taverna/products/uk.org.taverna.server/trunk/SOAP-API.html /taverna/products/uk.org.taverna.server/trunk/pom.xml /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/interfaces/Database.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/interfaces/DirectoryEntry.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/FileSegment.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/TavernaServerREST.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/TavernaServerRunREST.java /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/webapp/WEB-INF/providers.xml ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/AccessDeniedHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +import org.springframework.security.access.AccessDeniedException; + +public class AccessDeniedHandler extends HandlerCore implements + ExceptionMapper<AccessDeniedException> { + @Override + public Response toResponse(AccessDeniedException exception) { + return respond(FORBIDDEN, exception); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/BadInputPortNameHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.NOT_FOUND; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.BadInputPortNameException; + +@Provider +public class BadInputPortNameHandler extends HandlerCore implements + ExceptionMapper<BadInputPortNameException> { + @Override + public Response toResponse(BadInputPortNameException exn) { + return respond(NOT_FOUND, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/BadPropertyValueHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.BadPropertyValueException; + +@Provider +public class BadPropertyValueHandler extends HandlerCore implements + ExceptionMapper<BadPropertyValueException> { + @Override + public Response toResponse(BadPropertyValueException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/BadStateChangeHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.BadStateChangeException; + +@Provider +public class BadStateChangeHandler extends HandlerCore implements + ExceptionMapper<BadStateChangeException> { + @Override + public Response toResponse(BadStateChangeException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/FileMessageHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static org.taverna.server.master.TavernaServerImpl.log; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.FilesystemAccessException; +import org.taverna.server.master.interfaces.File; + +/** + * How to write out a File object with JAX-RS. + * + * @author Donal Fellows + */ +@Provider +public class FileMessageHandler implements MessageBodyWriter<File> { + /** How much to pull from the worker in one read. */ + private int maxChunkSize; + + /** + * @param maxChunkSize + * How much to pull from the worker in one read. + */ + public void setMaxChunkSize(int maxChunkSize) { + this.maxChunkSize = maxChunkSize; + } + + @Override + public boolean isWriteable(Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return File.class.isAssignableFrom(type); + } + + @Override + public long getSize(File t, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + try { + return t.getSize(); // Is it really raw bytes? + } catch (FilesystemAccessException e) { + log.info("failed to get file length", e); + return -1; + } + } + + @Override + public void writeTo(File t, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, Object> httpHeaders, + OutputStream entityStream) throws IOException, + WebApplicationException { + try { + int off = 0; + while (true) { + byte[] buffer = t.getContents(off, maxChunkSize); + if (buffer == null || buffer.length == 0) + break; + entityStream.write(buffer); + off += buffer.length; + } + } catch (FilesystemAccessException e) { + throw new IOException("problem when reading file", e); + } + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/FileSegmentHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static java.lang.Math.min; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.FilesystemAccessException; +import org.taverna.server.master.rest.FileSegment; + +/** + * How to write out a segment of a file with JAX-RS. + * + * @author Donal Fellows + */ +@Provider +public class FileSegmentHandler implements MessageBodyWriter<FileSegment> { + /** How much to pull from the worker in one read. */ + private int maxChunkSize; + + /** + * @param maxChunkSize + * How much to pull from the worker in one read. + */ + public void setMaxChunkSize(int maxChunkSize) { + this.maxChunkSize = maxChunkSize; + } + + @Override + public boolean isWriteable(Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return FileSegment.class.isAssignableFrom(type); + } + + @Override + public long getSize(FileSegment t, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return t.to - t.from; + } + + @Override + public void writeTo(FileSegment t, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, Object> httpHeaders, + OutputStream entityStream) throws IOException, + WebApplicationException { + try { + int off = t.from; + while (off < t.to) { + byte[] buffer = t.file.getContents(off, + min(maxChunkSize, t.to - off)); + if (buffer == null || buffer.length == 0) + break; + entityStream.write(buffer); + off += buffer.length; + } + } catch (FilesystemAccessException e) { + throw new IOException("problem when reading file", e); + } + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/FilesystemAccessHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.FilesystemAccessException; + +@Provider +public class FilesystemAccessHandler extends HandlerCore implements + ExceptionMapper<FilesystemAccessException> { + @Override + public Response toResponse(FilesystemAccessException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/GeneralFailureHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +import org.taverna.server.master.exceptions.GeneralFailureException; + +public class GeneralFailureHandler extends HandlerCore implements + ExceptionMapper<GeneralFailureException> { + @Override + public Response toResponse(GeneralFailureException exception) { + return respond(INTERNAL_SERVER_ERROR, exception); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/HandlerCore.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE; +import static javax.ws.rs.core.Response.status; +import static org.taverna.server.master.TavernaServerImpl.log; + +import javax.ws.rs.core.Response; + +import org.taverna.server.master.ManagementModel; + +/** + * Base class for handlers that grants Spring-enabled access to the management + * model. + * + * @author Donal Fellows + */ +public class HandlerCore { + private ManagementModel managementModel; + + /** + * @param managementModel + * the managementModel to set + */ + public void setManagementModel(ManagementModel managementModel) { + this.managementModel = managementModel; + } + + /** + * Simplified interface for building responses. + * + * @param status + * What status code to use? + * @param exception + * What exception to report on? + * @return The build response. + */ + protected Response respond(Response.Status status, Exception exception) { + if (managementModel.getLogOutgoingExceptions() + || status.getStatusCode() >= 500) + log.info("converting exception to response", exception); + return status(status).type(TEXT_PLAIN_TYPE) + .entity(exception.getMessage()).build(); + } + + /** + * Simplified interface for building responses. + * + * @param status + * What status code to use? + * @param partialMessage + * The prefix to the message. + * @param exception + * What exception to report on? + * @return The build response. + */ + protected Response respond(Response.Status status, String partialMessage, + Exception exception) { + if (managementModel.getLogOutgoingExceptions() + || status.getStatusCode() >= 500) + log.info("converting exception to response", exception); + return status(status).type(TEXT_PLAIN_TYPE) + .entity(partialMessage + "\n" + exception.getMessage()).build(); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/IllegalArgumentHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.UNSUPPORTED_MEDIA_TYPE; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +@Provider +public class IllegalArgumentHandler extends HandlerCore implements + ExceptionMapper<IllegalArgumentException> { + @Override + public Response toResponse(IllegalArgumentException exn) { + return respond(UNSUPPORTED_MEDIA_TYPE, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/ImplementationProblemHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +import org.taverna.server.localworker.remote.ImplementationException; + +public class ImplementationProblemHandler extends HandlerCore implements + ExceptionMapper<ImplementationException> { + @Override + public Response toResponse(ImplementationException exception) { + return respond(INTERNAL_SERVER_ERROR, exception); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/InputStreamMessageHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.List; + +import javax.ws.rs.Consumes; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.Provider; + +/** + * Maps a stream from a client into a bounded ordinary input stream that the + * webapp can work with more easily. + * + * @author Donal Fellows + */ +@Provider +@Consumes(APPLICATION_OCTET_STREAM) +public class InputStreamMessageHandler implements + MessageBodyReader<InputStream> { + @Override + public boolean isReadable(Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return InputStream.class.isAssignableFrom(type); + } + + @Override + public InputStream readFrom(Class<InputStream> type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, String> httpHeaders, InputStream entityStream) + throws IOException, WebApplicationException { + return new TransferStream(entityStream, + httpHeaders.get("Content-Length")); + } +} + +class TransferStream extends InputStream { + public TransferStream(InputStream entityStream, List<String> limit) { + this.entityStream = new BufferedInputStream(entityStream); + this.limit = limit != null && limit.size() > 0 ? Long.parseLong(limit + .get(0)) : -1; + } + + InputStream entityStream; + long limit; + long doneBytes = 0; + + @Override + public int read() throws IOException { + doneBytes++; + if (limit >= 0 && doneBytes >= limit) + return -1; + return entityStream.read(); + } + + @Override + public int read(byte[] ary, int off, int len) throws IOException { + if (limit >= 0) { + if (doneBytes >= limit) + return -1; + if (doneBytes + len > limit) + len = (int) (limit - doneBytes); + } + int readBytes = entityStream.read(ary, off, len); + if (readBytes > 0) + doneBytes += readBytes; + return readBytes; + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/InvalidCredentialHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.InvalidCredentialException; + +@Provider +public class InvalidCredentialHandler extends HandlerCore implements + ExceptionMapper<InvalidCredentialException> { + @Override + public Response toResponse(InvalidCredentialException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/JAXBExceptionHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; +import javax.xml.bind.JAXBException; + +@Provider +public class JAXBExceptionHandler extends HandlerCore implements + ExceptionMapper<JAXBException> { + @Override + public Response toResponse(JAXBException exn) { + return respond(FORBIDDEN, "APIEpicFail: " + exn.getErrorCode(), exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoCreateHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.NoCreateException; + +@Provider +public class NoCreateHandler extends HandlerCore implements + ExceptionMapper<NoCreateException> { + @Override + public Response toResponse(NoCreateException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoCredentialHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.NOT_FOUND; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +import org.taverna.server.master.exceptions.NoCredentialException; + +public class NoCredentialHandler extends HandlerCore implements + ExceptionMapper<NoCredentialException> { + @Override + public Response toResponse(NoCredentialException exn) { + return respond(NOT_FOUND, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoDestroyHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.NoDestroyException; + +@Provider +public class NoDestroyHandler extends HandlerCore implements + ExceptionMapper<NoDestroyException> { + @Override + public Response toResponse(NoDestroyException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoDirectoryEntryHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.NOT_FOUND; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.NoDirectoryEntryException; + +@Provider +public class NoDirectoryEntryHandler extends HandlerCore implements + ExceptionMapper<NoDirectoryEntryException> { + @Override + public Response toResponse(NoDirectoryEntryException exn) { + return respond(NOT_FOUND, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoListenerHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.NoListenerException; + +@Provider +public class NoListenerHandler extends HandlerCore implements + ExceptionMapper<NoListenerException> { + @Override + public Response toResponse(NoListenerException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NoUpdateHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.NoUpdateException; + +@Provider +public class NoUpdateHandler extends HandlerCore implements + ExceptionMapper<NoUpdateException> { + @Override + public Response toResponse(NoUpdateException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/NotOwnerHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.FORBIDDEN; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +import org.taverna.server.master.exceptions.NotOwnerException; + +public class NotOwnerHandler extends HandlerCore implements + ExceptionMapper<NotOwnerException> { + @Override + public Response toResponse(NotOwnerException exn) { + return respond(FORBIDDEN, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/PermissionHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.MessageBodyWriter; + +import org.taverna.server.master.common.Permission; + +/** + * Handler that allows CXF to send and receive {@linkplain Permission + * permissions} as plain text directly. + * + * @author Donal Fellows + */ +public class PermissionHandler implements MessageBodyReader<Permission>, + MessageBodyWriter<Permission> { + @Override + public boolean isWriteable(Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return type.isAssignableFrom(Permission.class) + && mediaType.isCompatible(TEXT_PLAIN_TYPE); + } + + @Override + public long getSize(Permission t, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return t.toString().length(); + } + + @Override + public void writeTo(Permission t, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, Object> httpHeaders, + OutputStream entityStream) throws IOException, + WebApplicationException { + new OutputStreamWriter(entityStream).write(t.toString()); + } + + @Override + public boolean isReadable(Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return type.isAssignableFrom(Permission.class) + && mediaType.isCompatible(TEXT_PLAIN_TYPE); + } + + @Override + public Permission readFrom(Class<Permission> type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, String> httpHeaders, InputStream entityStream) + throws IOException, WebApplicationException { + char[] cbuf = new char[7]; + int len = new InputStreamReader(entityStream).read(cbuf); + if (len < 0) + throw new IllegalArgumentException("no entity supplied"); + return Permission.valueOf(new String(cbuf, 0, len)); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/T2FlowDocumentHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.taverna.server.master.common.Workflow; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +/** + * Handler that allows a .t2flow document to be read from and written to a REST + * message directly. + * + * @author Donal Fellows + */ +@Provider +public class T2FlowDocumentHandler implements MessageBodyReader<Workflow>, + MessageBodyWriter<Workflow> { + private static final MediaType T2FLOW_TYPE = new MediaType("application", + "vnd.taverna.t2flow+xml"); + public static final String T2FLOW = "application/vnd.taverna.t2flow+xml"; + private static final String T2FLOW_ROOTNAME = "workflow"; + private static final String T2FLOW_NS = "http://taverna.sf.net/2008/xml/t2flow"; + private DocumentBuilder db; + private Transformer transformer; + + public T2FlowDocumentHandler() throws ParserConfigurationException, + TransformerConfigurationException { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setNamespaceAware(true); + db = dbf.newDocumentBuilder(); + TransformerFactory transFactory = TransformerFactory.newInstance(); + transformer = transFactory.newTransformer(); + } + + @Override + public boolean isReadable(Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + if (type.isAssignableFrom(Workflow.class)) + return mediaType.isCompatible(T2FLOW_TYPE); + return false; + } + + @Override + public Workflow readFrom(Class<Workflow> type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, String> httpHeaders, InputStream entityStream) + throws IOException, WebApplicationException { + Document doc; + try { + doc = db.parse(entityStream); + } catch (SAXException e) { + throw new WebApplicationException(e, 403); + } + Workflow workflow = new Workflow(); + workflow.content = new Element[] { doc.getDocumentElement() }; + if (doc.getDocumentElement().getNamespaceURI().equals(T2FLOW_NS) + && doc.getDocumentElement().getNodeName() + .equals(T2FLOW_ROOTNAME)) + return workflow; + throw new WebApplicationException(Response.status(403) + .entity("invalid T2flow document; bad root element") + .type("text/plain").build()); + } + + @Override + public boolean isWriteable(Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + if (Workflow.class.isAssignableFrom(type)) + return mediaType.isCompatible(T2FLOW_TYPE); + return false; + } + + @Override + public long getSize(Workflow workflow, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return -1; + } + + @Override + public void writeTo(Workflow workflow, Class<?> type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap<String, Object> httpHeaders, + OutputStream entityStream) throws IOException, + WebApplicationException { + try { + transformer.transform(new DOMSource(workflow.content[0]), + new StreamResult(entityStream)); + } catch (TransformerException e) { + throw new WebApplicationException(e); + } + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/UnknownRunHandler.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2010-2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +package org.taverna.server.master.rest.handler; + +import static javax.ws.rs.core.Response.Status.NOT_FOUND; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.taverna.server.master.exceptions.UnknownRunException; + +@Provider +public class UnknownRunHandler extends HandlerCore implements + ExceptionMapper<UnknownRunException> { + @Override + public Response toResponse(UnknownRunException exn) { + return respond(NOT_FOUND, exn); + } +} ======================================= --- /dev/null +++ /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/handler/package-info.java Fri Sep 23 07:12:57 2011 @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2011 The University of Manchester + * + * See the file "LICENSE.txt" for license terms. + */ +/** + * This package contains type handlers for the RESTful interface to Taverna Server. + * @author Donal Fellows + */ +@XmlSchema(namespace = SERVER_REST, elementFormDefault = QUALIFIED, attributeFormDefault = QUALIFIED, xmlns = { + @XmlNs(prefix = "xlink", namespaceURI = XLINK), + @XmlNs(prefix = "ts", namespaceURI = SERVER), + @XmlNs(prefix = "ts-rest", namespaceURI = SERVER_REST), + @XmlNs(prefix = "ts-soap", namespaceURI = SERVER_SOAP), + @XmlNs(prefix = "port", namespaceURI = DATA), + @XmlNs(prefix = "feed", namespaceURI = FEED), + @XmlNs(prefix = "admin", namespaceURI = ADMIN) }) +package org.taverna.server.master.rest.handler; + +import static javax.xml.bind.annotation.XmlNsForm.QUALIFIED; +import static org.taverna.server.master.common.Namespaces.ADMIN; +import static org.taverna.server.master.common.Namespaces.FEED; +import static org.taverna.server.master.common.Namespaces.SERVER; +import static org.taverna.server.master.common.Namespaces.SERVER_REST; +import static org.taverna.server.master.common.Namespaces.SERVER_SOAP; +import static org.taverna.server.master.common.Namespaces.XLINK; +import static org.taverna.server.port_description.Namespaces.DATA; + +import javax.xml.bind.annotation.XmlNs; +import javax.xml.bind.annotation.XmlSchema; + ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/AccessDeniedHandler.java Wed Jul 13 02:33:52 2011 +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2010-2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.FORBIDDEN; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; - -import org.springframework.security.access.AccessDeniedException; - -public class AccessDeniedHandler extends HandlerCore implements - ExceptionMapper<AccessDeniedException> { - @Override - public Response toResponse(AccessDeniedException exception) { - return respond(FORBIDDEN, exception); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/BadInputPortNameHandler.java Wed Jul 13 02:33:52 2011 +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2010-2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.NOT_FOUND; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -import org.taverna.server.master.exceptions.BadInputPortNameException; - -@Provider -public class BadInputPortNameHandler extends HandlerCore implements - ExceptionMapper<BadInputPortNameException> { - @Override - public Response toResponse(BadInputPortNameException exn) { - return respond(NOT_FOUND, exn); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/BadPropertyValueHandler.java Wed Jul 13 02:33:52 2011 +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2010-2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.FORBIDDEN; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -import org.taverna.server.master.exceptions.BadPropertyValueException; - -@Provider -public class BadPropertyValueHandler extends HandlerCore implements - ExceptionMapper<BadPropertyValueException> { - @Override - public Response toResponse(BadPropertyValueException exn) { - return respond(FORBIDDEN, exn); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/BadStateChangeHandler.java Wed Jul 13 02:33:52 2011 +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2010-2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.FORBIDDEN; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -import org.taverna.server.master.exceptions.BadStateChangeException; - -@Provider -public class BadStateChangeHandler extends HandlerCore implements - ExceptionMapper<BadStateChangeException> { - @Override - public Response toResponse(BadStateChangeException exn) { - return respond(FORBIDDEN, exn); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/FileMessageHandler.java Wed Jul 13 02:33:52 2011 +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2010-2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static org.taverna.server.master.TavernaServerImpl.log; - -import java.io.IOException; -import java.io.OutputStream; -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; - -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.ext.MessageBodyWriter; -import javax.ws.rs.ext.Provider; - -import org.taverna.server.master.exceptions.FilesystemAccessException; -import org.taverna.server.master.interfaces.File; - -/** - * How to write out a File object with JAX-RS. - * - * @author Donal Fellows - */ -@Provider -public class FileMessageHandler implements MessageBodyWriter<File> { - /** How much to pull from the worker in one read. */ - private int maxChunkSize; - - /** - * @param maxChunkSize - * How much to pull from the worker in one read. - */ - public void setMaxChunkSize(int maxChunkSize) { - this.maxChunkSize = maxChunkSize; - } - - @Override - public boolean isWriteable(Class<?> type, Type genericType, - Annotation[] annotations, MediaType mediaType) { - return File.class.isAssignableFrom(type); - } - - @Override - public long getSize(File t, Class<?> type, Type genericType, - Annotation[] annotations, MediaType mediaType) { - try { - return t.getSize(); // Is it really raw bytes? - } catch (FilesystemAccessException e) { - log.info("failed to get file length", e); - return -1; - } - } - - @Override - public void writeTo(File t, Class<?> type, Type genericType, - Annotation[] annotations, MediaType mediaType, - MultivaluedMap<String, Object> httpHeaders, - OutputStream entityStream) throws IOException, - WebApplicationException { - try { - int off = 0; - while (true) { - byte[] buffer = t.getContents(off, maxChunkSize); - if (buffer == null || buffer.length == 0) - break; - entityStream.write(buffer); - off += buffer.length; - } - } catch (FilesystemAccessException e) { - throw new IOException("problem when reading file", e); - } - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/FileSegmentHandler.java Tue Jul 19 06:39:49 2011 +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static java.lang.Math.min; - -import java.io.IOException; -import java.io.OutputStream; -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; - -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.ext.MessageBodyWriter; -import javax.ws.rs.ext.Provider; - -import org.taverna.server.master.exceptions.FilesystemAccessException; - -/** - * How to write out a segment of a file with JAX-RS. - * - * @author Donal Fellows - */ -@Provider -public class FileSegmentHandler implements MessageBodyWriter<FileSegment> { - /** How much to pull from the worker in one read. */ - private int maxChunkSize; - - /** - * @param maxChunkSize - * How much to pull from the worker in one read. - */ - public void setMaxChunkSize(int maxChunkSize) { - this.maxChunkSize = maxChunkSize; - } - - @Override - public boolean isWriteable(Class<?> type, Type genericType, - Annotation[] annotations, MediaType mediaType) { - return FileSegment.class.isAssignableFrom(type); - } - - @Override - public long getSize(FileSegment t, Class<?> type, Type genericType, - Annotation[] annotations, MediaType mediaType) { - return t.to - t.from; - } - - @Override - public void writeTo(FileSegment t, Class<?> type, Type genericType, - Annotation[] annotations, MediaType mediaType, - MultivaluedMap<String, Object> httpHeaders, - OutputStream entityStream) throws IOException, - WebApplicationException { - try { - int off = t.from; - while (off < t.to) { - byte[] buffer = t.file.getContents(off, - min(maxChunkSize, t.to - off)); - if (buffer == null || buffer.length == 0) - break; - entityStream.write(buffer); - off += buffer.length; - } - } catch (FilesystemAccessException e) { - throw new IOException("problem when reading file", e); - } - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/FilesystemAccessHandler.java Wed Jul 13 02:33:52 2011 +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2010-2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.FORBIDDEN; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -import org.taverna.server.master.exceptions.FilesystemAccessException; - -@Provider -public class FilesystemAccessHandler extends HandlerCore implements - ExceptionMapper<FilesystemAccessException> { - @Override - public Response toResponse(FilesystemAccessException exn) { - return respond(FORBIDDEN, exn); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/GeneralFailureHandler.java Fri Apr 15 08:20:33 2011 +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; - -import org.taverna.server.master.exceptions.GeneralFailureException; - -public class GeneralFailureHandler extends HandlerCore implements - ExceptionMapper<GeneralFailureException> { - @Override - public Response toResponse(GeneralFailureException exception) { - return respond(INTERNAL_SERVER_ERROR, exception); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/HandlerCore.java Tue Jul 19 05:19:00 2011 +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2010-2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE; -import static javax.ws.rs.core.Response.status; -import static org.taverna.server.master.TavernaServerImpl.log; - -import javax.ws.rs.core.Response; - -import org.taverna.server.master.ManagementModel; - -/** - * Base class for handlers that grants Spring-enabled access to the management - * model. - * - * @author Donal Fellows - */ -public class HandlerCore { - private ManagementModel managementModel; - - /** - * @param managementModel - * the managementModel to set - */ - public void setManagementModel(ManagementModel managementModel) { - this.managementModel = managementModel; - } - - /** - * Simplified interface for building responses. - * - * @param status - * What status code to use? - * @param exception - * What exception to report on? - * @return The build response. - */ - protected Response respond(Response.Status status, Exception exception) { - if (managementModel.getLogOutgoingExceptions() - || status.getStatusCode() >= 500) - log.info("converting exception to response", exception); - return status(status).type(TEXT_PLAIN_TYPE) - .entity(exception.getMessage()).build(); - } - - /** - * Simplified interface for building responses. - * - * @param status - * What status code to use? - * @param partialMessage - * The prefix to the message. - * @param exception - * What exception to report on? - * @return The build response. - */ - protected Response respond(Response.Status status, String partialMessage, - Exception exception) { - if (managementModel.getLogOutgoingExceptions() - || status.getStatusCode() >= 500) - log.info("converting exception to response", exception); - return status(status).type(TEXT_PLAIN_TYPE) - .entity(partialMessage + "\n" + exception.getMessage()).build(); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/IllegalArgumentHandler.java Thu Mar 17 04:03:23 2011 +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.UNSUPPORTED_MEDIA_TYPE; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -@Provider -public class IllegalArgumentHandler extends HandlerCore implements - ExceptionMapper<IllegalArgumentException> { - @Override - public Response toResponse(IllegalArgumentException exn) { - return respond(UNSUPPORTED_MEDIA_TYPE, exn); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/ImplementationProblemHandler.java Fri Apr 15 08:20:33 2011 +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; - -import org.taverna.server.localworker.remote.ImplementationException; - -public class ImplementationProblemHandler extends HandlerCore implements - ExceptionMapper<ImplementationException> { - @Override - public Response toResponse(ImplementationException exception) { - return respond(INTERNAL_SERVER_ERROR, exception); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/InputStreamMessageHandler.java Wed Jul 13 02:33:52 2011 +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2010-2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; -import java.util.List; - -import javax.ws.rs.Consumes; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.ext.MessageBodyReader; -import javax.ws.rs.ext.Provider; - -/** - * Maps a stream from a client into a bounded ordinary input stream that the - * webapp can work with more easily. - * - * @author Donal Fellows - */ -@Provider -@Consumes(APPLICATION_OCTET_STREAM) -public class InputStreamMessageHandler implements - MessageBodyReader<InputStream> { - @Override - public boolean isReadable(Class<?> type, Type genericType, - Annotation[] annotations, MediaType mediaType) { - return InputStream.class.isAssignableFrom(type); - } - - @Override - public InputStream readFrom(Class<InputStream> type, Type genericType, - Annotation[] annotations, MediaType mediaType, - MultivaluedMap<String, String> httpHeaders, InputStream entityStream) - throws IOException, WebApplicationException { - return new TransferStream(entityStream, - httpHeaders.get("Content-Length")); - } -} - -class TransferStream extends InputStream { - public TransferStream(InputStream entityStream, List<String> limit) { - this.entityStream = new BufferedInputStream(entityStream); - this.limit = limit != null && limit.size() > 0 ? Long.parseLong(limit - .get(0)) : -1; - } - - InputStream entityStream; - long limit; - long doneBytes = 0; - - @Override - public int read() throws IOException { - doneBytes++; - if (limit >= 0 && doneBytes >= limit) - return -1; - return entityStream.read(); - } - - @Override - public int read(byte[] ary, int off, int len) throws IOException { - if (limit >= 0) { - if (doneBytes >= limit) - return -1; - if (doneBytes + len > limit) - len = (int) (limit - doneBytes); - } - int readBytes = entityStream.read(ary, off, len); - if (readBytes > 0) - doneBytes += readBytes; - return readBytes; - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/InvalidCredentialHandler.java Mon Jan 10 07:50:01 2011 +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.FORBIDDEN; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -import org.taverna.server.master.exceptions.InvalidCredentialException; - -@Provider -public class InvalidCredentialHandler extends HandlerCore implements - ExceptionMapper<InvalidCredentialException> { - @Override - public Response toResponse(InvalidCredentialException exn) { - return respond(FORBIDDEN, exn); - } -} ======================================= --- /taverna/products/uk.org.taverna.server/trunk/server-webapp/src/main/java/org/taverna/server/master/rest/JAXBExceptionHandler.java Wed Jul 13 02:33:52 2011 +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2011 The University of Manchester - * - * See the file "LICENSE.txt" for license terms. - */ -package org.taverna.server.master.rest; - -import static javax.ws.rs.core.Response.Status.FORBIDDEN; - -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; -import javax.xml.bind.JAXBException; - -@Provider -public class JAXBExceptionHandler extends HandlerCore implements - ExceptionMapper<JAXBException> { - @Override - public Response toResponse(JAXBException exn) { - return respond(FORBIDDEN, "APIEpicFail: " + exn.getErrorCode(), exn); - } -} ======================================= --- /taverna/... [truncated message content] |