001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2005-2007 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.xmla;
011
012import org.w3c.dom.Element;
013
014import java.util.Map;
015import javax.servlet.ServletConfig;
016import javax.servlet.ServletException;
017import javax.servlet.http.HttpServletRequest;
018import javax.servlet.http.HttpServletResponse;
019
020
021/**
022 * Extract data from HTTP request, SOAP header for following XML/A request.<p/>
023 *
024 * Fill context binding with whatever data you want, then use them in
025 * {@link XmlaServlet#handleSoapHeader} and {@link XmlaServlet#handleSoapBody}.
026 *
027 * @author Gang Chen
028 */
029public interface XmlaRequestCallback {
030    String AUTHORIZATION = "Authorization";
031    String EXPECT = "Expect";
032    String EXPECT_100_CONTINUE = "100-continue";
033
034    public class Helper {
035        public static XmlaException authorizationException(Exception ex) {
036            return new XmlaException(
037                XmlaConstants.CLIENT_FAULT_FC,
038                XmlaConstants.CHH_AUTHORIZATION_CODE,
039                XmlaConstants.CHH_AUTHORIZATION_FAULT_FS,
040                ex);
041        }
042
043        /*
044    HTTP/1.1 100 Continue
045    Server: Microsoft-IIS/5.0
046    Date: Tue, 21 Feb 2006 21:07:57 GMT
047    X-Powered-By: ASP.NET
048        */
049        public static void generatedExpectResponse(
050            HttpServletRequest request,
051            HttpServletResponse response,
052            Map<String, Object> context) throws Exception
053        {
054            response.reset();
055            response.setStatus(HttpServletResponse.SC_CONTINUE);
056        }
057    }
058
059    void init(ServletConfig servletConfig) throws ServletException;
060
061    /**
062     * Process the request header items. Specifically if present the
063     * Authorization and Expect headers. If the Authorization header is
064     * present, then the callback can validate the user/password. If
065     * authentication fails, the callback should throw an XmlaException
066     * with the correct XmlaConstants values. The XmlaRequestCallback.Helper
067     * class contains the authorizationException method that can be used
068     * by a callback to generate the XmlaException with the correct values.
069     * If the Expect header is set with "100-continue", then it is
070     * upto the callback to create the appropriate response and return false.
071     * In this case, the XmlaServlet stops processing and returns the
072     * response to the client application. To facilitate the generation of
073     * the response, the XmlaRequestCallback.Helper has the method
074     * generatedExpectResponse that can be called by the callback.
075     * <p>
076     * Note that it is upto the XMLA client to determine whether or not
077     * there is an Expect header entry (ADOMD.NET seems to like to do this).
078     *
079     * @return true if XmlaServlet handling is to continue and false if
080     *         there was an Expect header "100-continue".
081     */
082    boolean processHttpHeader(
083        HttpServletRequest request,
084        HttpServletResponse response,
085        Map<String, Object> context) throws Exception;
086
087    /**
088     * This is called after the headers have been process but before the
089     * body (DISCOVER/EXECUTE) has been processed.
090     *
091     */
092    void preAction(
093        HttpServletRequest request,
094        Element[] requestSoapParts,
095        Map<String, Object> context) throws Exception;
096
097    /**
098     * The Callback is requested to generate a sequence id string. This
099     * sequence id was requested by the XMLA client and will be used
100     * for all subsequent communications in the Soap Header block.
101     *
102     * Implementation can return <code>null</code> if they do not want
103     * to generate a custom session ID, in which case, the default algorithm
104     * to generate session IDs will be used.
105     * @param context The context of this query.
106     * @return An arbitrary session id to use, or <code>null</code>.
107     */
108    String generateSessionId(Map<String, Object> context);
109
110    /**
111     * This is called after all Mondrian processing (DISCOVER/EXECUTE) has
112     * occurred.
113     *
114     */
115    void postAction(
116        HttpServletRequest request,
117        HttpServletResponse response,
118        byte[][] responseSoapParts,
119        Map<String, Object> context) throws Exception;
120}
121
122// End XmlaRequestCallback.java