01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.wso2.esb.transport.tomcat;
20:
21: /*
22: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
23: *
24: * Licensed under the Apache License, Version 2.0 (the "License");
25: * you may not use this file except in compliance with the License.
26: * You may obtain a copy of the License at
27: *
28: * http://www.apache.org/licenses/LICENSE-2.0
29: *
30: * Unless required by applicable law or agreed to in writing, software
31: * distributed under the License is distributed on an "AS IS" BASIS,
32: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33: * See the License for the specific language governing permissions and
34: * limitations under the License.
35: */
36:
37: import org.wso2.esb.ServiceBusManager;
38: import org.wso2.utils.NetworkUtils;
39:
40: import javax.servlet.*;
41: import javax.servlet.http.HttpServletRequest;
42: import javax.servlet.http.HttpServletResponse;
43: import java.io.IOException;
44:
45: /**
46: *
47: */
48:
49: public class RequestRedirectionFilter implements Filter {
50:
51: private String contextRoot;
52: private FilterConfig filterConfig;
53:
54: public void init(FilterConfig filterConfig) throws ServletException {
55: this .filterConfig = filterConfig;
56: filterConfig.getServletContext().setAttribute(
57: this .getClass().getName(), this );
58: }
59:
60: public void doFilter(ServletRequest request,
61: ServletResponse response, FilterChain filterChain)
62: throws IOException, ServletException {
63: HttpServletRequest httpServletRequest = (HttpServletRequest) request;
64: HttpServletResponse httpServletResponse = (HttpServletResponse) response;
65:
66: initContextRoot(httpServletRequest);
67: if (httpServletRequest.getRequestURL().toString().indexOf(
68: NetworkUtils.getLocalHostname()) == -1) {
69: httpServletResponse.sendRedirect("https://"
70: + NetworkUtils.getLocalHostname()
71: + ":"
72: + ServiceBusManager.getInstance().getHttpsPort()
73: + (contextRoot.equals("/") ? contextRoot
74: : contextRoot + "/")); // set the redirection
75: } else {
76: filterChain.doFilter(request, response);
77: }
78:
79: }
80:
81: public void destroy() {
82: this .filterConfig = null;
83: }
84:
85: private void initContextRoot(HttpServletRequest request) {
86:
87: if (contextRoot != null && contextRoot.length() != 0) {
88: return;
89: }
90:
91: String contextRoot = request.getContextPath();
92: if (contextRoot != null && contextRoot.length() == 0) {
93: contextRoot = "/";
94: }
95: this.contextRoot = contextRoot;
96: }
97: }
|