1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*******************************************************************************
* Copyright (c) 2014 Raymond Augé and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Raymond Augé <raymond.auge@liferay.com> - Bug 436698
******************************************************************************/
package org.eclipse.equinox.http.servlet.tests.tb1;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.equinox.http.servlet.tests.tb.AbstractTestServlet;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.http.NamespaceException;
import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
/*
* This servlet is registered with the HttpService via the immediate DS
* component OSGI-INF/testServlet1_component.xml.
*/
public class TestErrorPage1 extends AbstractTestServlet {
private static final long serialVersionUID = 1L;
private final Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
@Override
public void activate(ComponentContext componentContext) throws ServletException, NamespaceException {
Dictionary<String, String> servletProps = new Hashtable<String, String>();
servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S1");
servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, regexAlias());
registrations.add(componentContext.getBundleContext().registerService(Servlet.class, this, servletProps));
Dictionary<String, Object> errorProps = new Hashtable<String, Object>();
errorProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "E1");
errorProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, new String[] {"403", "404"});
registrations.add(componentContext.getBundleContext().registerService(Servlet.class, errorServlet, errorProps));
}
@Override
public void deactivate() {
for (ServiceRegistration<?> registration : registrations) {
registration.unregister();
}
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException ,IOException {
response.sendError(403);
}
Servlet errorServlet = new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
protected void service(
HttpServletRequest request, HttpServletResponse response)
throws ServletException ,IOException {
if (response.isCommitted()) {
System.out.println("Problem?");
return;
}
PrintWriter writer = response.getWriter();
String requestURI = (String)request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);
Integer status = (Integer)request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
writer.print(status + " ERROR : " + requestURI);
}
};
}
|