xref: /JGit/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/TestRequestLog.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
151e2646dSJens Baumgart /*
2*5c5f7c6bSMatthias Sohn  * Copyright (C) 2010, Google Inc. and others
351e2646dSJens Baumgart  *
4*5c5f7c6bSMatthias Sohn  * This program and the accompanying materials are made available under the
5*5c5f7c6bSMatthias Sohn  * terms of the Eclipse Distribution License v. 1.0 which is available at
6*5c5f7c6bSMatthias Sohn  * https://www.eclipse.org/org/documents/edl-v10.php.
751e2646dSJens Baumgart  *
8*5c5f7c6bSMatthias Sohn  * SPDX-License-Identifier: BSD-3-Clause
951e2646dSJens Baumgart  */
1051e2646dSJens Baumgart 
1151e2646dSJens Baumgart package org.eclipse.jgit.junit.http;
1251e2646dSJens Baumgart 
1351e2646dSJens Baumgart import java.io.IOException;
1451e2646dSJens Baumgart import java.util.ArrayList;
1551e2646dSJens Baumgart import java.util.List;
1651e2646dSJens Baumgart import java.util.concurrent.Semaphore;
1751e2646dSJens Baumgart 
18a24b7c3cSMatthias Sohn import javax.servlet.DispatcherType;
1951e2646dSJens Baumgart import javax.servlet.ServletException;
2051e2646dSJens Baumgart import javax.servlet.http.HttpServletRequest;
2151e2646dSJens Baumgart import javax.servlet.http.HttpServletResponse;
2251e2646dSJens Baumgart 
23d9e07a57SRobin Rosenberg import org.eclipse.jetty.server.Request;
24d9e07a57SRobin Rosenberg import org.eclipse.jetty.server.Response;
25d9e07a57SRobin Rosenberg import org.eclipse.jetty.server.handler.HandlerWrapper;
26d9e07a57SRobin Rosenberg 
2751e2646dSJens Baumgart /** Logs request made through {@link AppServer}. */
2851e2646dSJens Baumgart class TestRequestLog extends HandlerWrapper {
2951e2646dSJens Baumgart 	private static final int MAX = 16;
3051e2646dSJens Baumgart 
313b444863SDavid Pursehouse 	private final List<AccessEvent> events = new ArrayList<>();
3251e2646dSJens Baumgart 
33152d5e2aSMatthias Sohn 	private final Semaphore active = new Semaphore(MAX, true);
3451e2646dSJens Baumgart 
3551e2646dSJens Baumgart 	/** Reset the log back to its original empty state. */
clear()3651e2646dSJens Baumgart 	void clear() {
3751e2646dSJens Baumgart 		try {
3851e2646dSJens Baumgart 			for (;;) {
3951e2646dSJens Baumgart 				try {
4051e2646dSJens Baumgart 					active.acquire(MAX);
4151e2646dSJens Baumgart 					break;
4251e2646dSJens Baumgart 				} catch (InterruptedException e) {
4351e2646dSJens Baumgart 					continue;
4451e2646dSJens Baumgart 				}
4551e2646dSJens Baumgart 			}
4651e2646dSJens Baumgart 
4751e2646dSJens Baumgart 			synchronized (events) {
4851e2646dSJens Baumgart 				events.clear();
4951e2646dSJens Baumgart 			}
5051e2646dSJens Baumgart 		} finally {
5151e2646dSJens Baumgart 			active.release(MAX);
5251e2646dSJens Baumgart 		}
5351e2646dSJens Baumgart 	}
5451e2646dSJens Baumgart 
5551e2646dSJens Baumgart 	/** @return all of the events made since the last clear. */
getEvents()5651e2646dSJens Baumgart 	List<AccessEvent> getEvents() {
5751e2646dSJens Baumgart 		try {
5851e2646dSJens Baumgart 			for (;;) {
5951e2646dSJens Baumgart 				try {
6051e2646dSJens Baumgart 					active.acquire(MAX);
6151e2646dSJens Baumgart 					break;
6251e2646dSJens Baumgart 				} catch (InterruptedException e) {
6351e2646dSJens Baumgart 					continue;
6451e2646dSJens Baumgart 				}
6551e2646dSJens Baumgart 			}
6651e2646dSJens Baumgart 
6751e2646dSJens Baumgart 			synchronized (events) {
6851e2646dSJens Baumgart 				return events;
6951e2646dSJens Baumgart 			}
7051e2646dSJens Baumgart 		} finally {
7151e2646dSJens Baumgart 			active.release(MAX);
7251e2646dSJens Baumgart 		}
7351e2646dSJens Baumgart 	}
7451e2646dSJens Baumgart 
750b131b73SMatthias Sohn 	/** {@inheritDoc} */
7651e2646dSJens Baumgart 	@Override
handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)77520f3af7SMatthias Sohn 	public void handle(String target, Request baseRequest,
78520f3af7SMatthias Sohn 			HttpServletRequest request, HttpServletResponse response)
79520f3af7SMatthias Sohn 			throws IOException, ServletException {
8051e2646dSJens Baumgart 		try {
8151e2646dSJens Baumgart 			for (;;) {
8251e2646dSJens Baumgart 				try {
8351e2646dSJens Baumgart 					active.acquire();
8451e2646dSJens Baumgart 					break;
8551e2646dSJens Baumgart 				} catch (InterruptedException e) {
8651e2646dSJens Baumgart 					continue;
8751e2646dSJens Baumgart 				}
8851e2646dSJens Baumgart 			}
8951e2646dSJens Baumgart 
9051e2646dSJens Baumgart 			super.handle(target, baseRequest, request, response);
9151e2646dSJens Baumgart 
9251e2646dSJens Baumgart 			if (DispatcherType.REQUEST.equals(baseRequest.getDispatcherType()))
9351e2646dSJens Baumgart 				log((Request) request, (Response) response);
9451e2646dSJens Baumgart 
9551e2646dSJens Baumgart 		} finally {
9651e2646dSJens Baumgart 			active.release();
9751e2646dSJens Baumgart 		}
9851e2646dSJens Baumgart 	}
9951e2646dSJens Baumgart 
log(Request request, Response response)10051e2646dSJens Baumgart 	private void log(Request request, Response response) {
10151e2646dSJens Baumgart 		synchronized (events) {
10251e2646dSJens Baumgart 			events.add(new AccessEvent(request, response));
10351e2646dSJens Baumgart 		}
10451e2646dSJens Baumgart 	}
10551e2646dSJens Baumgart }
106