J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4160200 |
| 27 | * @summary Make sure URLConnection.getContnentHandler |
| 28 | * can handle MIME types with attributes |
| 29 | */ |
| 30 | import java.net.*; |
| 31 | import java.io.*; |
| 32 | import sun.net.www.content.text.*; |
| 33 | import sun.net.www.MessageHeader; |
| 34 | |
| 35 | public class HandleContentTypeWithAttrs { |
| 36 | |
| 37 | URL url; |
| 38 | |
| 39 | public HandleContentTypeWithAttrs (int port) throws Exception { |
| 40 | |
| 41 | String localHostName = InetAddress.getLocalHost().getHostName(); |
| 42 | |
| 43 | // Request echo.html from myHttpServer. |
| 44 | // In the header of the response, we make |
| 45 | // the content type have some attributes. |
| 46 | url = new URL("http://" + localHostName + ":" + port + "/echo.html"); |
| 47 | URLConnection urlConn = url.openConnection(); |
| 48 | |
| 49 | // the method getContent() calls the method |
| 50 | // getContentHandler(). With the fix, the method |
| 51 | // getContentHandler() gets the correct content |
| 52 | // handler for our response - it should be the |
| 53 | // PlainText conten handler; without the fix, |
| 54 | // it gets the UnknownContent handler. |
| 55 | // So based on what the getContent() |
| 56 | // returns, we know whether getContentHandler() |
| 57 | // can handle content type with attributes or not. |
| 58 | Object obj = urlConn.getContent(); |
| 59 | |
| 60 | if (!(obj instanceof PlainTextInputStream)) |
| 61 | throw new Exception("Cannot get the correct content handler."); |
| 62 | } |
| 63 | |
| 64 | public static void main(String [] argv) throws Exception { |
| 65 | // Start myHttpServer |
| 66 | myHttpServer testServer = new myHttpServer(); |
| 67 | testServer.startServer(0); |
| 68 | int serverPort = testServer.getServerLocalPort(); |
| 69 | new HandleContentTypeWithAttrs(serverPort); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // myHttpServer is pretty much like |
| 74 | // sun.net.www.httpd.BasicHttpServer. |
| 75 | // But we only need it to handle one |
| 76 | // simple request. |
| 77 | class myHttpServer implements Runnable, Cloneable { |
| 78 | |
| 79 | /** Socket for communicating with client. */ |
| 80 | public Socket clientSocket = null; |
| 81 | private Thread serverInstance; |
| 82 | private ServerSocket serverSocket; |
| 83 | |
| 84 | /** Stream for printing to the client. */ |
| 85 | public PrintStream clientOutput; |
| 86 | |
| 87 | /** Buffered stream for reading replies from client. */ |
| 88 | public InputStream clientInput; |
| 89 | |
| 90 | static URL defaultContext; |
| 91 | |
| 92 | /** Close an open connection to the client. */ |
| 93 | public void close() throws IOException { |
| 94 | clientSocket.close(); |
| 95 | clientSocket = null; |
| 96 | clientInput = null; |
| 97 | clientOutput = null; |
| 98 | } |
| 99 | |
| 100 | final public void run() { |
| 101 | if (serverSocket != null) { |
| 102 | Thread.currentThread().setPriority(Thread.MAX_PRIORITY); |
| 103 | |
| 104 | try { |
| 105 | // wait for incoming request |
| 106 | Socket ns = serverSocket.accept(); |
| 107 | myHttpServer n = (myHttpServer)clone(); |
| 108 | n.serverSocket = null; |
| 109 | n.clientSocket = ns; |
| 110 | new Thread(n).start(); |
| 111 | } catch(Exception e) { |
| 112 | System.out.print("Server failure\n"); |
| 113 | e.printStackTrace(); |
| 114 | try { |
| 115 | serverSocket.close(); |
| 116 | } catch(IOException e2) {} |
| 117 | } |
| 118 | } else { |
| 119 | try { |
| 120 | clientOutput = new PrintStream( |
| 121 | new BufferedOutputStream(clientSocket.getOutputStream()), |
| 122 | false); |
| 123 | clientInput = new BufferedInputStream( |
| 124 | clientSocket.getInputStream()); |
| 125 | serviceRequest(); |
| 126 | |
| 127 | } catch(Exception e) { |
| 128 | // System.out.print("Service handler failure\n"); |
| 129 | // e.printStackTrace(); |
| 130 | } |
| 131 | try { |
| 132 | close(); |
| 133 | } catch(IOException e2) {} |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** Start a server on port <i>port</i>. It will call serviceRequest() |
| 138 | for each new connection. */ |
| 139 | final public void startServer(int port) throws IOException { |
| 140 | serverSocket = new ServerSocket(port, 50); |
| 141 | serverInstance = new Thread(this); |
| 142 | serverInstance.start(); |
| 143 | } |
| 144 | |
| 145 | final public int getServerLocalPort() throws Exception { |
| 146 | if (serverSocket != null) { |
| 147 | return serverSocket.getLocalPort(); |
| 148 | } |
| 149 | throw new Exception("serverSocket is null"); |
| 150 | } |
| 151 | |
| 152 | MessageHeader mh; |
| 153 | |
| 154 | final public void serviceRequest() { |
| 155 | //totalConnections++; |
| 156 | try { |
| 157 | mh = new MessageHeader(clientInput); |
| 158 | String cmd = mh.findValue(null); |
| 159 | // if (cmd == null) { |
| 160 | // error("Missing command " + mh); |
| 161 | // return; |
| 162 | // } |
| 163 | int fsp = cmd.indexOf(' '); |
| 164 | // if (fsp < 0) { |
| 165 | // error("Syntax error in command: " + cmd); |
| 166 | // return; |
| 167 | // } |
| 168 | String k = cmd.substring(0, fsp); |
| 169 | int nsp = cmd.indexOf(' ', fsp + 1); |
| 170 | String p1, p2; |
| 171 | if (nsp > 0) { |
| 172 | p1 = cmd.substring(fsp + 1, nsp); |
| 173 | p2 = cmd.substring(nsp + 1); |
| 174 | } else { |
| 175 | p1 = cmd.substring(fsp + 1); |
| 176 | p2 = null; |
| 177 | } |
| 178 | // expectsMime = p2 != null; |
| 179 | if (k.equalsIgnoreCase("get")) |
| 180 | getRequest(new URL(defaultContext, p1), p2); |
| 181 | else { |
| 182 | // error("Unknown command: " + k + " (" + cmd + ")"); |
| 183 | return; |
| 184 | } |
| 185 | } catch(IOException e) { |
| 186 | // totally ignore IOException. They're usually client crashes. |
| 187 | } catch(Exception e) { |
| 188 | // error("Exception: " + e); |
| 189 | e.printStackTrace(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** Satisfy one get request. It is invoked with the clientInput and |
| 194 | clientOutput streams initialized. This method handles one client |
| 195 | connection. When it is done, it can simply exit. The default |
| 196 | server just echoes it's input. */ |
| 197 | protected void getRequest(URL u, String param) { |
| 198 | try { |
| 199 | if (u.getFile().equals("/echo.html")) { |
| 200 | startHtml("Echo reply"); |
| 201 | clientOutput.print("<p>URL was " + u.toExternalForm() + "\n"); |
| 202 | clientOutput.print("<p>Socket was " + clientSocket + "\n<p><pre>"); |
| 203 | mh.print(clientOutput); |
| 204 | } |
| 205 | } catch(Exception e) { |
| 206 | System.out.print("Failed on "+u.getFile()+"\n"); |
| 207 | e.printStackTrace(); |
| 208 | } |
| 209 | } |
| 210 | /** |
| 211 | * Clone this object; |
| 212 | */ |
| 213 | public Object clone() { |
| 214 | try { |
| 215 | return super.clone(); |
| 216 | } catch (CloneNotSupportedException e) { |
| 217 | // this shouldn't happen, since we are Cloneable |
| 218 | throw new InternalError(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | public myHttpServer () { |
| 223 | try { |
| 224 | defaultContext |
| 225 | = new URL("http", InetAddress.getLocalHost().getHostName(), "/"); |
| 226 | } catch(Exception e) { |
| 227 | System.out.println("Failed to construct defauit URL context: " |
| 228 | + e); |
| 229 | e.printStackTrace(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Make the content type have some attributes |
| 234 | protected void startHtml(String title) { |
| 235 | clientOutput.print("HTTP/1.0 200 Document follows\n" + |
| 236 | "Server: Java/" + getClass().getName() + "\n" + |
| 237 | "Content-type: text/plain; charset=Shift_JIS \n\n"); |
| 238 | } |
| 239 | |
| 240 | } |