Changeset 7216


Ignore:
Timestamp:
10/21/18 17:40:59 (6 years ago)
Author:
Leif-Jöran
Message:

Fixing x-aggregation-context dispatching/handover once and for all.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • SRUAggregator/trunk/src/main/java/eu/clarin/sru/fcs/aggregator/app/ErrorHandler.java

    r6153 r7216  
    66import javax.servlet.http.HttpServletRequest;
    77import javax.servlet.http.HttpServletResponse;
     8import javax.servlet.http.HttpServletRequestWrapper;
     9//import org.apache.commons.lang3.StringEscapeUtils;
    810import org.eclipse.jetty.http.HttpMethod;
    911import org.eclipse.jetty.server.Request;
     
    1214/**
    1315 * @author edima
     16 * @author ljo
    1417 *
    1518 * The Aggregator is a single web page application, and most of the URLs are
     
    3134        public static final String PARAM_AGGREGATION_CONTEXT = "x-aggregation-context";
    3235
    33         public static final String redirectRoute = "/index.html";
     36        public static final String redirectRoute = "/";
    3437
    3538
     
    3841                // On 404 page we need to show index.html and let JS router do the work, otherwise show error page
    3942                if (response.getStatus() == HttpServletResponse.SC_NOT_FOUND) {
    40                         forward(redirectRoute, baseRequest, response);
     43                        forward(redirectRoute, baseRequest, request, response);
    4144                } else if (request.getMethod().equals("POST")
    4245                                && response.getStatus() == HttpServletResponse.SC_METHOD_NOT_ALLOWED) {
     
    4548                                String[] queryValues = request.getParameterValues(PARAM_QUERY);
    4649                                if (queryValues != null && queryValues.length > 0) {
    47                                         baseRequest.getSession().setAttribute(PARAM_QUERY, queryValues[0]);
     50                                        request.getSession().setAttribute(PARAM_QUERY, queryValues[0]);
    4851                                }
    4952                        }
     
    5255                                String[] modeValues = request.getParameterValues(PARAM_MODE);
    5356                                if (modeValues != null && modeValues.length > 0) {
    54                                         baseRequest.getSession().setAttribute(PARAM_MODE, modeValues[0]);
     57                                        request.getSession().setAttribute(PARAM_MODE, modeValues[0]);
    5558                                }
    5659                        }
     
    5962                                String[] contextValues = request.getParameterValues(PARAM_AGGREGATION_CONTEXT);
    6063                                if (contextValues != null && contextValues.length > 0) {
    61                                         baseRequest.getSession().setAttribute(PARAM_AGGREGATION_CONTEXT, contextValues[0]);
     64                                    request.getSession().setAttribute(PARAM_AGGREGATION_CONTEXT, contextValues[0]);
     65                                    request.setAttribute(PARAM_AGGREGATION_CONTEXT, contextValues[0]);
     66                                    request.setAttribute(PARAM_AGGREGATION_CONTEXT, contextValues[0]);
     67                                        log.info("Error handler request.contextValues > 0: " + contextValues[0]);
    6268                                }
     69                               
    6370                        }
    6471
    65                         baseRequest.setMethod(HttpMethod.GET, HttpMethod.GET.asString());
     72                        HttpServletRequest getRequest = new HttpServletRequestWrapper(request) {
     73                                @Override
     74                                public String getMethod(){
     75                                    return HttpMethod.GET.asString();
     76                                }
     77                            };
     78           
     79                        //baseRequest.setMethod(HttpMethod.GET, HttpMethod.GET.asString());
    6680
    67                         forward(redirectRoute, baseRequest, response);
     81                        forward(redirectRoute, baseRequest, getRequest, response);
    6882                } else {
    6983                        super.handle(target, baseRequest, request, response);
     
    7185        }
    7286
    73         void forward(String target, Request request, HttpServletResponse response) throws IOException {
    74                 RequestDispatcher dispatcher = request.getRequestDispatcher(target);
     87        void forward(final String target, final Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
     88                StringBuilder params = new StringBuilder();
     89                if (request.getSession().getAttribute(PARAM_AGGREGATION_CONTEXT) != null) {
     90                    params.append("x-aggregation-context=" + request.getSession().getAttribute(PARAM_AGGREGATION_CONTEXT));
     91                }
     92                RequestDispatcher dispatcher = request.getServletContext().getNamedDispatcher("static");
     93                request = new HttpServletRequestWrapper(request) {
     94                    @Override
     95                    public String getPathInfo(){
     96                        return target;
     97                    }
     98                };
    7599                if (dispatcher != null) {
     100                    request.setAttribute(dispatcher.FORWARD_QUERY_STRING, params.toString());
     101                    request.getSession().setAttribute(dispatcher.FORWARD_QUERY_STRING, params.toString());
    76102                        try {
     103                                log.info("dispatching attribute check:" + request.getSession().getAttribute(PARAM_AGGREGATION_CONTEXT) + " " + request.getSession());
    77104                                response.reset();
    78105                                dispatcher.forward(request, response);
    79106                        } catch (ServletException e) {
    80                                 super.handle(target, request, request, response);
     107                                log.info("dispatching failed, attribute check:" + request.getSession().getAttribute(PARAM_AGGREGATION_CONTEXT));
     108                                super.handle(target, baseRequest, request, response);
    81109                        }
    82110                } else {
    83111                        log.error("Can not find internal redirect route '" + target + "' while handling error. Will show system error page");
    84                         super.handle(target, request, request, response);
     112                        super.handle(target, baseRequest, request, response);
    85113                }
    86114        }
Note: See TracChangeset for help on using the changeset viewer.