001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver;
006:
007: import java.io.BufferedReader;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.InputStreamReader;
011: import java.io.OutputStreamWriter;
012: import java.net.HttpURLConnection;
013: import java.net.MalformedURLException;
014: import java.net.URL;
015: import java.util.Date;
016:
017: class TestGetThread extends Thread {
018: private static final boolean concurrent = false;
019: URL url;
020: int result = 0;
021: Date t1;
022: Date t2;
023: Date t3;
024: protected HttpURLConnection hc;
025:
026: TestGetThread(URL u) throws MalformedURLException {
027: url = new URL(u.toString());
028: }
029:
030: int getResult() {
031: return result;
032: }
033:
034: public void connect() throws IOException {
035: //HttpURLConnection hc = null;
036: hc = (HttpURLConnection) url.openConnection();
037: hc.setRequestMethod("GET");
038: t1 = new Date();
039: yield(); //wait to let everyone else connect before getting result
040: hc.connect();
041: //return hc;
042: }
043:
044: public void run() {
045: try {
046: //HttpURLConnection hc = connect();
047: //Unsure if we want this, this is an experiment to try to connect
048: //more than once, which is what a reasonable web browser would
049: //do I believe. Or maybe web browsers only try once? It's a bit
050: //imperfect, as the input stream stuff should probably be here too
051: //since if this is running it just shifts more errors to BindExceptions
052: //when hc.getInputStream is called, since it's connected but not yet
053: //bound - it doesn't have the actual stream yet. So perhaps it needs
054: //to be in the loop too, though would that cause it to get a new
055: //connection (from the connect method) if it failed? And/or is that
056: //maybe the behavior that we want? Or do we want it to try to bind
057: //on the same connection - ch. (should also make these options
058: //user configurable...hmmm - how did I get roped in to writing
059: //a java wfs client? This is essentially what the wfsTester that
060: //rob wrote does, and it's about as naive, it just doesn't do
061: //threads. If anyone wants to expand on this the one nice thing
062: //to have, that rob never got to, would be some validation
063: //as well. Shouldn't be too hard, just run xerces schema
064: //checking on it. But then again this class was written to test
065: //a ton of requests at once, not to be a full tester. One thing
066: //that would be cool though is to take a list of requests and
067: //fire them all off at different intervals. Though I feel like
068: //we might be able to find some better framework to do that,
069: //rather than just writing our own.
070: int tries = 0;
071: while (tries < 15) {
072: try {
073: connect();
074: break;
075: } catch (IOException ioe) {
076: System.out.println("IOException: "
077: + ioe.getMessage() + ", try: " + tries);
078:
079: sleep(5);
080: tries++;
081: }
082: }
083:
084: t2 = new Date();
085:
086: InputStream input = hc.getInputStream();
087: BufferedReader br = new BufferedReader(
088: new InputStreamReader(input));
089:
090: if (concurrent) {
091: yield();
092: }
093:
094: //This reading bit seems to be damned if you do damned if you dont
095: //The first way ensures that the client doesn't prematurely close
096: //the socket, which happens on big GetFeature requests all the
097: //time, I'm not too sure why, perhaps it gets set to not ready
098: //when the underlying stream from the server pauses or something.
099:
100: //The second way ensures that we don't get errors with the input
101: //stream closing on us, but for some reason I can't seem to reproduce
102: //them right now - the only reason I can think of is because I
103: //changed it to explicitly create the InputStream, so it doesn't
104: //get gced or something? I'm not too sure though, but as the first
105: //isn't messing up at all we'll stick with it, as the second (original)
106: //way wasn't actually reading everything, so it was returning false
107: //response times.
108:
109: //Oh ok, it seems that the exception is only on resin, with ready
110: //it works with getCaps, but with read it messes up about one in
111: //40 or so with java.net.SocketException: socket closed - so it
112: //looks like resin decides to close the socket prematurely under
113: //heavy duress or something. I'm not sure, but I definitely feel
114: //like I'm testing servlet containers far more than GeoServer
115: //itself with this solid day of testing.
116:
117: while (br.read() != -1)
118: ;
119:
120: //while(br.ready())
121: // br.readLine();
122: result = hc.getResponseCode();
123: t3 = new Date();
124:
125: yield(); //wait to let everyone else hit before disconnecting
126: hc.disconnect();
127: ThreadedBatchTester.threadDone();
128: } catch (Exception e) {
129: e.printStackTrace();
130: try {
131: result = hc.getResponseCode();
132: } catch (IOException ioe) {
133: result = 0;
134: }
135: ThreadedBatchTester.threadDone();
136: }
137: }
138:
139: Date getTime1() {
140: return t1;
141: }
142:
143: Date getTime2() {
144: return t2;
145: }
146:
147: Date getTime3() {
148: return t3;
149: }
150: }
151:
152: class TestPostThread extends TestGetThread {
153: String request;
154:
155: TestPostThread(URL u, String request) throws MalformedURLException {
156: super (u);
157: this .request = request;
158: }
159:
160: public void connect() throws IOException {
161: //HttpURLConnection hc = null;
162: hc = (HttpURLConnection) url.openConnection();
163: hc.setRequestMethod("POST");
164: hc.setDoOutput(true);
165: t1 = new Date();
166: yield(); //wait to let everyone else connect before getting result
167: hc.connect();
168:
169: OutputStreamWriter osw = new OutputStreamWriter(hc
170: .getOutputStream());
171: osw.write(request);
172: osw.flush();
173: }
174:
175: /*public void run(){
176: try{
177: HttpURLConnection hc = null;
178: hc = (HttpURLConnection)url.openConnection();
179: hc.setRequestMethod("POST");
180: hc.setDoOutput(true);
181: yield(); //wait to let everyone else connect before getting result
182:
183: t1 = new Date();
184: hc.connect();
185: OutputStreamWriter osw = new OutputStreamWriter(hc.getOutputStream());
186: osw.write(request);
187: osw.flush();
188:
189: t2 = new Date();
190: BufferedReader br = new BufferedReader(new InputStreamReader(hc.getInputStream()));
191: while(br.ready()){
192: br.readLine();
193: }
194: result = hc.getResponseCode();
195: t3 = new Date();
196: yield(); //wait to let everyone else hit before disconnecting
197: hc.disconnect();
198: }catch(Exception e){
199: e.printStackTrace();
200: result = 0;
201: }
202: }*/
203: }
|