KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > uihandler > MemoryURL


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.uihandler;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLConnection JavaDoc;
29 import java.net.URLStreamHandler JavaDoc;
30 import java.net.URLStreamHandlerFactory JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import junit.framework.Assert;
34
35 /**
36  *
37  * @author Jaroslav Tulach
38  */

39 public class MemoryURL extends URLStreamHandler JavaDoc {
40
41     static {
42         class F implements URLStreamHandlerFactory JavaDoc {
43             public URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
44                 if (protocol.startsWith("memory")) {
45                     return new MemoryURL();
46                 }
47                 return null;
48             }
49         }
50         F f = new F();
51         URL.setURLStreamHandlerFactory(f);
52     }
53     
54     private static Map JavaDoc<String JavaDoc,InputStream JavaDoc> contents = new HashMap JavaDoc<String JavaDoc,InputStream JavaDoc>();
55     private static Map JavaDoc<String JavaDoc,MC> outputs = new HashMap JavaDoc<String JavaDoc,MC>();
56     public static void registerURL(String JavaDoc u, String JavaDoc content) {
57         contents.put(u, new ByteArrayInputStream JavaDoc(content.getBytes()));
58     }
59     public static void registerURL(String JavaDoc u, InputStream JavaDoc content) {
60         contents.put(u, content);
61     }
62     
63     public static byte[] getOutputForURL(String JavaDoc u) {
64         MC out = outputs.get(u);
65         Assert.assertNotNull("No output for " + u, out);
66         return out.out.toByteArray();
67     }
68     
69     public static String JavaDoc getRequestParameter(String JavaDoc u, String JavaDoc param) {
70         MC out = outputs.get(u);
71         Assert.assertNotNull("No output for " + u, out);
72         return out.params.get(param.toLowerCase());
73     }
74
75     protected URLConnection JavaDoc openConnection(URL JavaDoc u) throws IOException JavaDoc {
76         return new MC(u);
77     }
78     
79     private static final class MC extends URLConnection JavaDoc {
80         private InputStream JavaDoc values;
81         private ByteArrayOutputStream JavaDoc out;
82         private Map JavaDoc<String JavaDoc,String JavaDoc> params;
83         
84         public MC(URL JavaDoc u) {
85             super(u);
86             outputs.put(u.toExternalForm(), this);
87             params = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
88         }
89
90         public void connect() throws IOException JavaDoc {
91             if (values != null) {
92                 return;
93             }
94             values = contents.remove(url.toExternalForm());
95             if (values == null) {
96                 throw new IOException JavaDoc("No such content: " + url);
97             }
98         }
99
100         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
101             connect();
102             return values;
103         }
104
105         public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
106             if (out == null) {
107                 out = new ByteArrayOutputStream JavaDoc();
108             }
109             return out;
110         }
111
112         public void setRequestProperty(String JavaDoc key, String JavaDoc value) {
113             super.setRequestProperty(key, value);
114             params.put(key.toLowerCase(), value);
115         }
116         
117         
118     }
119 }
120
Popular Tags