// Copyright 2012 Georg-August-Universität Göttingen, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package de.ugoe.cs.autoquest.httpmonitor.proxy; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Timer; import java.util.TimerTask; import java.util.logging.Level; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent; import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException; import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler; import de.ugoe.cs.autoquest.httpmonitor.exchange.Status; import de.ugoe.cs.util.console.Console; /** *
* {@link ExchangeListener}s need to be managed, as there is one instantiated for each proxied * request/response. They need to be timed out, if for a longer time, no update is received. All * this is done by the ExchangeListenerManager. *
* * @author Patrick Harms */ class ExchangeListenerManager implements HttpMonitorComponent { /** ** the timeout after which a writer of an inactive client is closed *
*/ private static final int SESSION_TIMEOUT = 10 * 60 * 1000; /** ** the exchange handler forwarded to the exchange listeners *
*/ private HttpMonitorExchangeHandler exchangeHandler; /** ** the mapping of requests handled by the proxy to the respective exchange listeners *
*/ private Map* a timer used to detect exchange listener timeouts *
*/ private Timer listenerTimer; /** ** creates the exchange listener manager with the exchange handler to be forwarded to the * exchange listeners *
* * @param exchangeHandler the exchange handler to be forwarded to the exchange listeners */ ExchangeListenerManager(HttpMonitorExchangeHandler exchangeHandler) { this.exchangeHandler = exchangeHandler; } /* (non-Javadoc) * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init() */ @Override public synchronized void init() throws IllegalStateException, HttpMonitorException { Console.traceln(Level.FINER, "initializing exchange listener manager"); if (listeners != null) { throw new IllegalStateException("already initialized"); } listeners = new HashMap* called, when the request was received by the proxy. Calls the appropriate method on * the exchange listener. *
* * @param request the request of the exchange */ public void onRequest(HttpServletRequest request) throws IllegalStateException { ensureListener(request).onRequest(request); } /** ** called, when some content of the request was processed by the proxy. Calls the appropriate * method on the exchange listener. *
* * @param request the request of the exchange * @param data the processed content of the request of the exchange */ public void onRequestContent(HttpServletRequest request, ByteBuffer data) throws IllegalStateException { ensureListener(request).onRequestContent(data); } /** ** called, when the response is to be returned by the proxy. Calls the appropriate * method on the exchange listener. *
* * @param request the request of the exchange * @param response the response of the exchange */ public void onResponse(HttpServletRequest request, HttpServletResponse response) throws IllegalStateException { ensureListener(request).onResponse(response); } /** ** called, when some content of the response was processed by the proxy. Calls the appropriate * method on the exchange listener. *
* * @param request the request of the exchange * @param data the processed content of the response of the exchange */ public void onResponseContent(HttpServletRequest request, ByteBuffer data) throws IllegalStateException { ensureListener(request).onResponseContent(data); } /** ** called, when proxy finished proxying a request. Calls the appropriate method on the * exchange listener and afterwards cleans up the listener. *
* * @param request the request of the exchange * @param status the status of the proxying after finalization */ public void onFinish(HttpServletRequest request, Status status) throws IllegalStateException { ensureListener(request).onFinish(status); Console.traceln(Level.FINEST, "removing exchange listener for " + request); listeners.remove(request); } /** ** convenience method to ensure a listener for a specific HTTP servlet request to be handled. *
*/ private ExchangeListener ensureListener(HttpServletRequest request) { ExchangeListener listener = listeners.get(request); if (listener == null) { synchronized (this) { listener = listeners.get(request); if (listener == null) { Console.traceln(Level.FINEST, "creating exchange listener for " + request); listener = new ExchangeListener(exchangeHandler); listeners.put(request, listener); } } } return listener; } /** ** internal timer task used for detecting exchange timeouts *
* * @author Patrick Harms */ public class ListenerMonitorTimerTask extends TimerTask { /* (non-Javadoc) * @see java.lang.Runnable#run() */ @Override public void run() { synchronized (ExchangeListenerManager.this) { List