/* Copyright 2013, 2015 Nationale-Nederlanden Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package nl.nn.adapterframework.http; import java.util.Map; import org.apache.commons.lang.StringUtils; import nl.nn.adapterframework.configuration.ConfigurationException; import nl.nn.adapterframework.configuration.HasSpecialDefaultValues; import nl.nn.adapterframework.core.HasPhysicalDestination; import nl.nn.adapterframework.core.ListenerException; /** * Implementation of a {@link nl.nn.adapterframework.core.IPushingListener IPushingListener} that enables a {@link nl.nn.adapterframework.receivers.GenericReceiver} * to receive REST messages. * <p><b>Configuration:</b> * <table border="1"> * <tr><th>attributes</th><th>description</th><th>default</th></tr> * <tr><td>className</td><td>nl.nn.adapterframework.http.RestListener</td><td> </td></tr> * <tr><td>{@link #setName(String) name}</td><td>name of the listener as known to the adapter</td><td> </td></tr> * <tr><td>{@link #setUriPattern(String) uriPattern}</td><td>uri pattern to match. </td><td> </td></tr> * <tr><td>{@link #setMethod(String) method}</td><td>method (e.g. GET or POST) to match</td><td> </td></tr> * <tr><td>{@link #setEtagSessionKey(String) etagSessionKey}</td><td>key of Session variable to store ETAG</td><td> </td></tr> * <tr><td>{@link #setContentTypeSessionKey(String) contentTypeSessionKey}</td><td>key of Session variable to requested content type</td><td> </td></tr> * <tr><td>{@link #setApplicationFaultsAsExceptions(boolean) applicationFaultsAsExceptions}</td><td>Controls the behavior when an application-fault occurrs: * <table> * <tr><td><code>true</code></td><td>errors are returned as a HTTP internal server error (500)</td></tr> * <tr><td><code>false</code></td><td>errors are returned in error message generated by the IBIS error message formatter</td></tr> * </table> * </td><td>true</td></tr> * <tr><td>{@link #setView(boolean) view}</td><td>indicates whether this listener supports a view (and a link should be put in the IBIS console)</td><td>if <code>method=GET</code> then <code>true</code>, else <code>false</code></td></tr> * <tr><td>{@link #seAuthRoles(String) authRoles}</td><td>comma separated list of authorization roles which are granted for this REST service</td><td>IbisAdmin,IbisDataAdmin,IbisTester,IbisObserver,IbisWebService</td></tr> * </table> * @author Gerrit van Brakel */ public class RestListener extends PushingListenerAdapter implements HasPhysicalDestination, HasSpecialDefaultValues { private String uriPattern; private String method; private String etagSessionKey; private String contentTypeSessionKey; private String restPath = "/rest"; private Boolean view = null; private String authRoles="IbisAdmin,IbisDataAdmin,IbisTester,IbisObserver,IbisWebService"; private boolean writeToSecLog = false; private boolean writeSecLogMessage = false; /** * initialize listener and register <code>this</code> to the JNDI */ public void configure() throws ConfigurationException { super.configure(); if (isView()==null) { if (StringUtils.isEmpty(getMethod()) || getMethod().equalsIgnoreCase("GET")) { setView(true); } else { setView(false); } } RestServiceDispatcher.getInstance().registerServiceClient(this, getUriPattern(), method, etagSessionKey, contentTypeSessionKey); } @Override public void close() { super.close(); RestServiceDispatcher.getInstance().unregisterServiceClient(getUriPattern()); } public String processRequest(String correlationId, String message, Map requestContext) throws ListenerException { String requestRestPath = (String) requestContext.get("restPath"); if (!getRestPath().equals(requestRestPath)) { throw new ListenerException("illegal restPath value [" + requestRestPath + "], must be '" + getRestPath() + "'"); } return super.processRequest(correlationId, message, requestContext); } public String getPhysicalDestinationName() { return "uriPattern: "+(getUriPattern()==null?"-any-":getUriPattern())+"; method: "+(getMethod()==null?"all":getMethod()); } public String getRestUriPattern() { return getRestPath().substring(1) + "/" + getUriPattern(); } public String getUriPattern() { return uriPattern; } public void setUriPattern(String uriPattern) { this.uriPattern = uriPattern; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getEtagSessionKey() { return etagSessionKey; } public void setEtagSessionKey(String etagSessionKey) { this.etagSessionKey = etagSessionKey; } public String getContentTypeSessionKey() { return contentTypeSessionKey; } public void setContentTypeSessionKey(String contentTypeSessionKey) { this.contentTypeSessionKey = contentTypeSessionKey; } public String getRestPath() { return restPath; } public void setRestPath(String restPath) { this.restPath = restPath; } public void setView(boolean b) { view = b; } public Boolean isView() { return view; } public Object getSpecialDefaultValue(String attributeName, Object defaultValue, Map<String, String> attributes) { if ("view".equals(attributeName)) { if (attributes.get("method").equalsIgnoreCase("GET")) { return true; } else { return false; } } return defaultValue; } public void setAuthRoles(String string) { authRoles = string; } public String getAuthRoles() { return authRoles; } public void setWriteToSecLog(boolean b) { writeToSecLog = b; } public boolean isWriteToSecLog() { return writeToSecLog; } public void setWriteSecLogMessage(boolean b) { writeSecLogMessage = b; } public boolean isWriteSecLogMessage() { return writeSecLogMessage; } }