Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2014 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.shared.test.pagedriver; import static org.junit.Assert.assertTrue; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.opentestsystem.shared.test.api.PageDriver; import org.opentestsystem.shared.test.interactioncontext.FastInteractionContext; import org.opentestsystem.shared.test.interactioncontext.FastInteractionResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class FastPageDriver implements PageDriver<FastInteractionResponse> { private static final Logger _logger = LoggerFactory.getLogger(FastPageDriver.class); private static final char[] NULL_CHARS = { '<', 'n', 'u', 'l', 'l', '>' }; private FastInteractionContext _interactionContext; private FastInteractionResponse _interactionResponse; /** * Stub implementation fails nothing */ @Override public void assertExpectedPage() throws AssertionError { } /** * Stub implementation. Alternate implementations may return an instance of a * subclass instead of "this" */ @Override public FastPageDriver init(final FastInteractionResponse interactionResponse, final boolean ensureLoaded) throws Throwable { _interactionContext = interactionResponse.getInteractionContext(); _interactionResponse = interactionResponse; if (ensureLoaded) { ensurePageLoaded(); } return this; } /** * Stub implementation does nothing */ @Override public void ensurePageLoaded() throws AssertionError { // Do nothing } /** * Return the interaction context within which this pate was loaded * * @return */ protected FastInteractionContext getInteractionContext() { return _interactionContext; } /** * Return the initial interaction response from which this page was loaded. * * @return */ protected FastInteractionResponse getInteractionResponse() { return _interactionResponse; } protected abstract String getTemplateBaseUrl(String resourceDirName, int i); protected void loadResourcesInLogs(String resourceDirName, int firstResource, int lastResource) throws AssertionError { // Waste time reading all the static files. long t_timeout = getInteractionResponse().getTimeoutTime(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); for (int i = firstResource; i <= lastResource; i++) { long t_left = t_timeout - System.currentTimeMillis(); if (t_left < 0) { throw new AssertionError("Login shell load took too long."); } String resourceName = String.format("student-requests/%s/%09d-request.txt", resourceDirName, i); try (InputStream is = cl.getResourceAsStream(resourceName); BufferedReader reader = new BufferedReader(new InputStreamReader(is))) { // Get method final String method = reader.readLine(); // Get the original URL, and map it to the current server final String originalURL = reader.readLine(); String templateBaseUrl = getTemplateBaseUrl(resourceDirName, i); assertTrue("Unexpected source URL in template file", originalURL.startsWith(templateBaseUrl)); final String path = originalURL.substring(templateBaseUrl.length()); final URL requestURL = new URL(getInteractionContext().getBaseUrl(), path); // Get the request headers String line = reader.readLine(); while (!StringUtils.equals("Headers:", line)) { line = reader.readLine(); } final Map<String, List<String>> headers = new HashMap<>(); while (!StringUtils.equals(line = reader.readLine(), "Body:")) { if (StringUtils.isBlank(line)) { continue; } String[] keyValuePair = StringUtils.split(line, ":", 2); if (keyValuePair.length != 2) { continue; } String key = StringUtils.strip(keyValuePair[0]); List<String> headerList = headers.get(key); if (headerList == null) { headerList = new ArrayList<>(); headers.put(key, headerList); } headerList.add(StringUtils.strip(keyValuePair[1])); } // Get the request body, if any reader.mark(6); char[] buffer = new char[6]; reader.read(buffer); if (Arrays.equals(NULL_CHARS, buffer)) { getInteractionContext().buildResponse(method, requestURL, headers, null, t_left); } else { reader.reset(); getInteractionContext().buildResponse(method, requestURL, headers, reader, t_left); } } catch (IOException | NullPointerException | AssertionError e) { _logger.warn( String.format("Error while parsing template file %s:\r\n%s ", resourceName, e.toString())); } } } }