1 package org.objectweb.celtix.bus.transports.http.protocol.pipe; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.OutputStream ; 6 import java.io.PipedInputStream ; 7 import java.io.PipedOutputStream ; 8 import java.net.URL ; 9 import java.net.URLConnection ; 10 import java.util.Arrays ; 11 import java.util.Collections ; 12 import java.util.HashMap ; 13 import java.util.List ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 public class PipeURLConnection extends URLConnection { 18 19 Map <String , List <String >> requestHeaders = new HashMap <String , List <String >>(); 20 PipedOutputStream pout; 21 PipeResponse response; 22 23 public PipeURLConnection(URL url) { 24 super(url); 25 } 26 27 public void connect() throws IOException { 28 PipedInputStream pin = new BiggerPipeInputStream(); 29 pout = new PipedOutputStream (pin); 30 response = PipeServer.startDispatch(url, this, pin, requestHeaders); 31 connected = true; 32 } 33 34 public InputStream getInputStream() throws IOException { 35 return response.getInputStream(getReadTimeout()); 36 } 37 38 public OutputStream getOutputStream() throws IOException { 39 if (!connected) { 40 connect(); 41 } 42 return pout; 43 } 44 45 private void setupReceive() throws IOException { 46 if (pout != null) { 47 pout.flush(); 48 pout.close(); 49 pout = null; 50 } 51 } 52 53 54 public void setRequestProperty(String key, String value) { 56 super.setRequestProperty(key, value); 57 requestHeaders.put(key, Arrays.asList(new String [] {value})); 58 } 59 public void addRequestProperty(String key, String value) { 60 super.addRequestProperty(key, value); 61 List <String > list = requestHeaders.get(key); 62 if (null == list) { 63 setRequestProperty(key, value); 64 } else { 65 list.add(value); 66 } 67 } 68 public String getRequestProperty(String key) { 69 super.getRequestProperty(key); 70 List <String > list = requestHeaders.get(key); 71 if (list != null && !list.isEmpty()) { 72 if (list.size() == 1) { 73 return list.get(0); 74 } else { 75 StringBuffer buf = new StringBuffer (list.get(0)); 76 for (int x = 1; x < list.size(); x++) { 77 buf.append(";"); 78 buf.append(list.get(x)); 79 } 80 return buf.toString(); 81 } 82 } 83 return null; 84 } 85 public Map <String , List <String >> getRequestProperties() { 86 return Collections.unmodifiableMap(requestHeaders); 87 } 88 89 90 91 public Map <String , List <String >> getHeaderFields() { 93 try { 94 setupReceive(); 95 return Collections.unmodifiableMap(response.getResponseHeaders(getReadTimeout())); 96 } catch (IOException e) { 97 return null; 98 } 99 } 100 public String getHeaderField(String name) { 101 try { 102 setupReceive(); 103 List <String > list = response.getResponseHeaders(getReadTimeout()).get(name); 104 if (list != null && !list.isEmpty()) { 105 if (list.size() == 1) { 106 return list.get(0); 107 } else { 108 StringBuffer buf = new StringBuffer (list.get(0)); 109 for (int x = 1; x < list.size(); x++) { 110 buf.append(";"); 111 buf.append(list.get(x)); 112 } 113 return buf.toString(); 114 } 115 } 116 } catch (IOException e) { 117 } 119 return null; 120 } 121 public String getHeaderFieldKey(int n) { 122 int x = 0; 123 Set <String > keys; 124 try { 125 setupReceive(); 126 keys = response.getResponseHeaders(getReadTimeout()).keySet(); 127 } catch (IOException e) { 128 return null; 129 } 130 for (String name : keys) { 131 if (x == n) { 132 return name; 133 } 134 ++x; 135 } 136 return null; 137 } 138 public String getHeaderField(int n) { 139 int x = 0; 140 Set <String > keys; 141 try { 142 setupReceive(); 143 keys = response.getResponseHeaders(getReadTimeout()).keySet(); 144 } catch (IOException e) { 145 return null; 146 } 147 for (String name : keys) { 148 if (x == n) { 149 return getHeaderField(name); 150 } 151 ++x; 152 } 153 return null; 154 } 155 156 157 158 private static class BiggerPipeInputStream extends PipedInputStream { 159 BiggerPipeInputStream() { 160 super(); 161 buffer = new byte[PIPE_SIZE * 4]; 162 } 163 } 164 } 165 | Popular Tags |