001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.servlet;
017:
018: import java.io.IOException;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.directwebremoting.extend.Remoter;
026: import org.directwebremoting.util.LocalUtil;
027: import org.directwebremoting.util.MimeConstants;
028:
029: /**
030: * A handler for interface generation requests
031: * @author Joe Walker [joe at getahead dot ltd dot uk]
032: */
033: public class InterfaceHandler extends JavaScriptHandler {
034: /**
035: * Setup the {@link JavaScriptHandler} defaults
036: */
037: public InterfaceHandler() {
038: setMimeType(MimeConstants.MIME_JS);
039: }
040:
041: /* (non-Javadoc)
042: * @see org.directwebremoting.servlet.TemplateHandler#generateTemplate(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
043: */
044: @Override
045: protected String generateTemplate(HttpServletRequest request,
046: HttpServletResponse response) throws IOException {
047: String scriptName = request.getPathInfo();
048: scriptName = scriptName.replace(interfaceHandlerUrl, "");
049: scriptName = scriptName.replace(PathConstants.EXTENSION_JS, "");
050: if (!LocalUtil.isJavaIdentifier(scriptName)) {
051: log.debug("Throwing at request for script with name: '"
052: + scriptName + "'");
053: throw new SecurityException(
054: "Script names may only contain Java Identifiers");
055: }
056:
057: String contextServletPath = request.getContextPath()
058: + request.getServletPath();
059:
060: return remoter.generateInterfaceScript(scriptName,
061: contextServletPath);
062: }
063:
064: /**
065: * Setter for the remoter
066: * @param remoter The new remoter
067: */
068: public void setRemoter(Remoter remoter) {
069: this .remoter = remoter;
070: }
071:
072: /**
073: * Setter for the URL that this handler available on
074: * @param interfaceHandlerUrl the interfaceHandlerUrl to set
075: */
076: public void setInterfaceHandlerUrl(String interfaceHandlerUrl) {
077: this .interfaceHandlerUrl = interfaceHandlerUrl;
078: }
079:
080: /* (non-Javadoc)
081: * @see java.lang.Object#toString()
082: */
083: @Override
084: public String toString() {
085: return "InterfaceHandler(" + interfaceHandlerUrl + ")";
086: }
087:
088: /**
089: * The bean to execute remote requests and generate interfaces
090: */
091: protected Remoter remoter = null;
092:
093: /**
094: * What URL is this handler available on?
095: */
096: protected String interfaceHandlerUrl;
097:
098: /**
099: * The log stream
100: */
101: private static final Log log = LogFactory
102: .getLog(InterfaceHandler.class);
103: }
|