1 | package eu.clarin.sru.fcs.aggregator.app; |
---|
2 | |
---|
3 | import com.fasterxml.jackson.databind.ObjectMapper; |
---|
4 | import java.io.IOException; |
---|
5 | import javax.servlet.RequestDispatcher; |
---|
6 | import javax.servlet.ServletException; |
---|
7 | import javax.servlet.http.HttpServletRequest; |
---|
8 | import javax.servlet.http.HttpServletResponse; |
---|
9 | import org.eclipse.jetty.http.HttpMethod; |
---|
10 | import org.eclipse.jetty.server.Request; |
---|
11 | import org.slf4j.LoggerFactory; |
---|
12 | |
---|
13 | /** |
---|
14 | * |
---|
15 | * @author edima |
---|
16 | */ |
---|
17 | public class ErrorHandler extends org.eclipse.jetty.server.handler.ErrorHandler { |
---|
18 | |
---|
19 | private static final org.slf4j.Logger log = LoggerFactory.getLogger(Aggregator.class); |
---|
20 | |
---|
21 | private final String redirectRoute = "/index.html"; |
---|
22 | |
---|
23 | @Override |
---|
24 | public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { |
---|
25 | // On 404 page we need to show index.html and let JS router do the work, otherwise show error page |
---|
26 | if (response.getStatus() == HttpServletResponse.SC_NOT_FOUND) { |
---|
27 | forward(redirectRoute, baseRequest, response); |
---|
28 | } else if (request.getMethod().equals("POST") |
---|
29 | && response.getStatus() == HttpServletResponse.SC_METHOD_NOT_ALLOWED |
---|
30 | && request.getParameterValues("x-aggregation-context") != null) { |
---|
31 | |
---|
32 | // this request must come from VLO |
---|
33 | String[] queryValues = request.getParameterValues("query"); |
---|
34 | if (queryValues != null && queryValues.length > 0) { |
---|
35 | baseRequest.getSession().setAttribute("query", queryValues[0]); |
---|
36 | } |
---|
37 | |
---|
38 | String[] contextValues = request.getParameterValues("x-aggregation-context"); |
---|
39 | if (contextValues != null && contextValues.length > 0) { |
---|
40 | baseRequest.getSession().setAttribute("x-aggregation-context", contextValues[0]); |
---|
41 | } |
---|
42 | |
---|
43 | baseRequest.setMethod(HttpMethod.GET, HttpMethod.GET.asString()); |
---|
44 | |
---|
45 | forward(redirectRoute, baseRequest, response); |
---|
46 | } else { |
---|
47 | super.handle(target, baseRequest, request, response); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | void forward(String target, Request request, HttpServletResponse response) throws IOException { |
---|
52 | RequestDispatcher dispatcher = request.getRequestDispatcher(target); |
---|
53 | if (dispatcher != null) { |
---|
54 | try { |
---|
55 | response.reset(); |
---|
56 | dispatcher.forward(request, response); |
---|
57 | } catch (ServletException e) { |
---|
58 | super.handle(target, request, request, response); |
---|
59 | } |
---|
60 | } else { |
---|
61 | log.error("Can not find internal redirect route '" + target + "' while handling error. Will show system error page"); |
---|
62 | super.handle(target, request, request, response); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|