001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.threadtest.tester;
018:
019: import java.util.Arrays;
020: import java.util.List;
021:
022: import org.apache.commons.httpclient.HttpClient;
023: import org.apache.commons.httpclient.methods.GetMethod;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.wicket.util.io.Streams;
027:
028: public class SimpleGetCommand extends AbstractGetCommand {
029:
030: private static final Log log = LogFactory
031: .getLog(SimpleGetCommand.class);
032:
033: private boolean printResponse = false;
034:
035: /**
036: * Construct.
037: *
038: * @param urls
039: * URLs to visit
040: * @param iterations
041: * number of executions of the urls
042: */
043: public SimpleGetCommand(List<String> urls, int iterations) {
044: super (urls, iterations);
045: }
046:
047: /**
048: * Construct.
049: *
050: * @param url
051: * URL to visit
052: * @param iterations
053: * number of executions of the urls
054: */
055: public SimpleGetCommand(String url, int iterations) {
056: super (Arrays.asList(new String[] { url }), iterations);
057: }
058:
059: /**
060: * Gets printResponse.
061: *
062: * @return printResponse
063: */
064: public boolean getPrintResponse() {
065: return printResponse;
066: }
067:
068: /**
069: * Sets printResponse.
070: *
071: * @param printResponse
072: * printResponse
073: */
074: public void setPrintResponse(boolean printResponse) {
075: this .printResponse = printResponse;
076: }
077:
078: /**
079: * @see org.apache.wicket.threadtest.tester.AbstractGetCommand#doGet(org.apache.commons.httpclient.HttpClient,
080: * java.lang.String)
081: */
082: @Override
083: protected void doGet(HttpClient client, String url)
084: throws Exception {
085:
086: GetMethod method = new GetMethod(url);
087: method.setFollowRedirects(true);
088: try {
089: int code = client.executeMethod(method);
090: if (code != 200) {
091: log.error("ERROR! code: " + code);
092: log.error(Streams.readString(method
093: .getResponseBodyAsStream()));
094: // TODO rather than failing, just gather statistics
095: // throw new Exception(new String(method.getResponseBody()));
096: }
097: if (getPrintResponse()) {
098: log.info("\n"
099: + Streams.readString(method
100: .getResponseBodyAsStream()));
101: }
102: } finally {
103: method.releaseConnection();
104: }
105: }
106: }
|