| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.util; |
| 16 | |
| 17 | import java.io.IOException; |
| 18 | |
| 19 | import javax.servlet.Filter; |
| 20 | import javax.servlet.FilterChain; |
| 21 | import javax.servlet.FilterConfig; |
| 22 | import javax.servlet.ServletException; |
| 23 | import javax.servlet.ServletRequest; |
| 24 | import javax.servlet.ServletResponse; |
| 25 | import javax.servlet.http.HttpServletRequest; |
| 26 | import javax.servlet.http.HttpServletResponse; |
| 27 | |
| 28 | import org.apache.hivemind.Registry; |
| 29 | import org.apache.hivemind.servlet.HiveMindFilter; |
| 30 | |
| 31 | import com.caucho.hessian.io.HessianOutput; |
| 32 | |
| 33 | /** |
| 34 | * This filter checks the version of the client and possibly return an exception |
| 35 | * through Hessian protocol. |
| 36 | * |
| 37 | * @author Jean-Francois Poilpret |
| 38 | */ |
| 39 | public class VersionControlFilter implements Filter |
| 40 | { |
| 41 | public void init(FilterConfig config) throws ServletException |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | public void destroy() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | public void doFilter( ServletRequest request, |
| 50 | ServletResponse response, |
| 51 | FilterChain chain) |
| 52 | throws IOException, ServletException |
| 53 | { |
| 54 | HttpServletRequest req = (HttpServletRequest) request; |
| 55 | HttpServletResponse res = (HttpServletResponse) response; |
| 56 | |
| 57 | init(req); |
| 58 | |
| 59 | // Check client version and set server version on response |
| 60 | Exception e = _controller.checkClientVersion(req, res); |
| 61 | |
| 62 | // If client version not supported |
| 63 | if (e == null) |
| 64 | { |
| 65 | chain.doFilter(request, response); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | // And serialize it through hessian |
| 70 | HessianOutput out = new HessianOutput(response.getOutputStream()); |
| 71 | out.startReply(); |
| 72 | out.writeFault("ServiceException", e.getMessage(), e); |
| 73 | out.completeReply(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | protected void init(HttpServletRequest request) |
| 78 | { |
| 79 | if (_controller == null) |
| 80 | { |
| 81 | initService(request); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | synchronized protected void initService(HttpServletRequest request) |
| 86 | { |
| 87 | if (_controller == null) |
| 88 | { |
| 89 | Registry registry = HiveMindFilter.getRegistry(request); |
| 90 | _controller = (VersionControlManager) registry.getService( |
| 91 | "hiveboard.server.VersionController", VersionControlManager.class); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private VersionControlManager _controller; |
| 96 | } |