1 5 6 package com.hp.hpl.jena.joseki; 7 8 import org.apache.commons.logging.* ; 9 import java.net.* ; 10 import java.io.* ; 11 12 import com.hp.hpl.jena.rdf.model.*; 13 import org.joseki.*; 14 15 25 26 public class HttpExecute 27 { 28 private static Log log = LogFactory.getLog(HttpExecute.class.getName()) ; 29 30 private URL url = null ; 31 32 private String requestMethod = null ; 33 private boolean doOutput = true ; 34 private boolean hasExecuted = false ; 35 36 static final String ENC_UTF8 = "UTF-8" ; 37 38 43 44 protected HttpExecute(String target, String opName) throws MalformedURLException 45 { init(target, opName) ; } 46 47 50 protected HttpExecute() { } 51 52 protected void init(String target, String opName) 53 throws MalformedURLException 54 { 55 url = new URL(target+"?op="+opName) ; 56 } 57 58 protected void setRequestMethod(String rMethod, boolean _doOutput) 60 { 61 requestMethod = rMethod ; 62 doOutput = _doOutput ; 63 } 64 65 protected void setURL(String urlStr) throws MalformedURLException 67 { 68 url = new URL(urlStr) ; 69 } 70 71 public Model exec() throws HttpException 72 { 73 HttpURLConnection conn = null ; 74 try 75 { 76 conn = (HttpURLConnection) url.openConnection(); 77 conn.setRequestProperty("Accept", Joseki.contentTypeRDFXML+", "+ 78 Joseki.contentTypeN3) ; 79 conn.setRequestProperty("Accept-Charset", ENC_UTF8) ; 80 81 if ( requestMethod != null ) 82 conn.setRequestMethod(requestMethod) ; 83 conn.setDoInput(true); 84 85 if ( doOutput ) 86 { 87 conn.setRequestProperty("Content-Type", Joseki.clientContentType+ "; charset="+ENC_UTF8) ; 89 conn.setDoOutput(true); 93 onSend(Joseki.clientContentType, conn.getOutputStream()); 94 conn.getOutputStream().flush() ; 95 } 96 conn.connect(); 97 } 98 catch (java.net.ConnectException connEx) 99 { 100 throw new HttpException(-1, "Failed to connect to remote server"); 101 } 102 103 catch (IOException ioEx) 104 { 105 throw new HttpException(ioEx); 106 } 107 catch (RDFException rdfEx) 108 { 109 throw new HttpException(rdfEx) ; 110 } 111 112 try { 113 int responseCode = conn.getResponseCode() ; 114 String responseMessage = conn.getResponseMessage() ; 115 116 122 if ( 300 <= responseCode && responseCode < 400 ) 123 { 124 throw new HttpException(responseCode, responseMessage) ; 125 } 126 127 129 if ( responseCode >= 400 ) 130 { 131 throw new HttpException(responseCode, responseMessage) ; 132 } 133 134 Model resultModel = onResult(conn.getContentType(), conn.getInputStream()) ; 137 return resultModel ; 138 } 139 catch (IOException ioEx) 140 { 141 log.info("IOException after connect: "+ioEx) ; 142 throw new HttpException(ioEx) ; 143 } 144 catch (RDFException rdfEx) 145 { 146 log.info("RDFException(result): "+rdfEx) ; 147 throw new HttpException(rdfEx) ; 148 } 149 } 150 151 156 157 protected void onSend(String mediaType, OutputStream out) { return ; } 158 159 protected Model onResult(String mediaType, InputStream in) 160 { 161 Model resultModel = ModelFactory.createDefaultModel() ; 163 String rType = Joseki.getWriterType(mediaType) ; 164 165 if ( false ) 166 { 167 try 168 { 169 log.trace("Reader type: "+rType) ; 170 171 byte b[] = new byte[1024] ; 172 ByteArrayOutputStream bout = new ByteArrayOutputStream() ; 173 while(true) 174 { 175 int len = in.read(b) ; 176 if ( len == -1 ) 177 break ; 178 bout.write(b, 0, len) ; 179 } 180 log.trace("\n"+bout.toString()) ; 182 183 in = new ByteArrayInputStream(bout.toByteArray()) ; 185 } 186 catch (IOException ioEx) { System.err.println("IOException: "+ioEx) ; return null ; } 187 } 188 resultModel.read(in, "http://somewhere/", Joseki.getWriterType(mediaType)) ; 189 hasExecuted = true ; 190 return resultModel ; 191 } 192 } 193 194 195 221 222 | Popular Tags |