blob: 0e3fb5186a5c80a6e1565074e23039f3657240c1 [file] [log] [blame]
/*
* Copyright (C) 2010-2011 The University of Manchester
*
* See the file "LICENSE" 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.apache.commons.logging.LogFactory.getLog;
import javax.ws.rs.core.Response;
import org.apache.commons.logging.Log;
import org.taverna.server.master.api.ManagementModel;
/**
* Base class for handlers that grants Spring-enabled access to the management
* model.
*
* @author Donal Fellows
*/
public class HandlerCore {
private Log log = getLog("Taverna.Server.Webapp");
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();
}
}