1 16 package org.mortbay.http; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 21 import org.apache.commons.logging.Log; 22 import org.mortbay.log.LogFactory; 23 import org.mortbay.util.StringUtil; 24 25 26 32 public class MultiPartResponse 33 { 34 private static Log log = LogFactory.getLog(MultiPartResponse.class); 35 36 37 private static byte[] __CRLF; 38 private static byte[] __DASHDASH; 39 static 40 { 41 try 42 { 43 __CRLF="\015\012".getBytes(StringUtil.__ISO_8859_1); 44 __DASHDASH="--".getBytes(StringUtil.__ISO_8859_1); 45 } 46 catch (Exception e) {log.fatal(e); System.exit(1);} 47 } 48 49 50 private String boundary; 51 private byte[] boundaryBytes; 52 53 54 private MultiPartResponse() 55 { 56 try 57 { 58 boundary = "jetty"+System.identityHashCode(this)+ 59 Long.toString(System.currentTimeMillis(),36); 60 boundaryBytes=boundary.getBytes(StringUtil.__ISO_8859_1); 61 } 62 catch (Exception e) 63 { 64 log.fatal(e); System.exit(1); 65 } 66 } 67 68 69 public String getBoundary() 70 { 71 return boundary; 72 } 73 74 75 77 private OutputStream out = null; 78 public OutputStream getOut() {return out;} 79 80 81 private boolean inPart=false; 82 83 84 public MultiPartResponse(OutputStream out) 85 throws IOException 86 { 87 this(); 88 this.out=out; 89 inPart=false; 90 } 91 92 93 95 public MultiPartResponse(HttpResponse response) 96 throws IOException 97 { 98 this(); 99 response.setField(HttpFields.__ContentType,"multipart/mixed;boundary="+boundary); 100 out=response.getOutputStream(); 101 inPart=false; 102 } 103 104 105 107 public void startPart(String contentType) 108 throws IOException 109 { 110 if (inPart) 111 out.write(__CRLF); 112 inPart=true; 113 out.write(__DASHDASH); 114 out.write(boundaryBytes); 115 out.write(__CRLF); 116 out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1)); 117 out.write(__CRLF); 118 out.write(__CRLF); 119 } 120 121 122 124 public void startPart(String contentType, String [] headers) 125 throws IOException 126 { 127 if (inPart) 128 out.write(__CRLF); 129 inPart=true; 130 out.write(__DASHDASH); 131 out.write(boundaryBytes); 132 out.write(__CRLF); 133 out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1)); 134 out.write(__CRLF); 135 for (int i=0;headers!=null && i<headers.length;i++) 136 { 137 out.write(headers[i].getBytes(StringUtil.__ISO_8859_1)); 138 out.write(__CRLF); 139 } 140 out.write(__CRLF); 141 } 142 143 144 147 public void close() 148 throws IOException 149 { 150 if (inPart) 151 out.write(__CRLF); 152 out.write(__DASHDASH); 153 out.write(boundaryBytes); 154 out.write(__DASHDASH); 155 out.write(__CRLF); 156 inPart=false; 157 } 158 159 }; 160 161 162 163 164 | Popular Tags |