1 19 20 21 package com.hp.hpl.jena.reasoner.dig; 24 25 26 27 import java.io.*; 30 import java.net.*; 31 import java.util.*; 32 33 import javax.xml.parsers.*; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.apache.xml.serialize.*; 38 import org.w3c.dom.*; 39 40 import com.hp.hpl.jena.util.FileUtils; 41 42 43 51 public class DIGConnection { 52 55 56 public static final String DEFAULT_REASONER_URL = "http://localhost:8081"; 57 58 59 public static final String XSI = "http://www.w3.org/2001/XMLSchema-instance"; 60 61 62 65 private static Log log = LogFactory.getLog( DIGConnection.class ); 66 67 68 71 72 protected String m_extReasonerURL = DEFAULT_REASONER_URL; 73 74 75 private String m_kbURI; 76 77 78 protected DocumentBuilderFactory m_factory = DocumentBuilderFactory.newInstance(); 79 80 81 private List m_warnings = new ArrayList(); 82 83 84 protected boolean m_logCommunications = true; 85 86 87 90 93 100 public Document sendDigVerb( Document digVerb, DIGProfile profile ) { 101 try { 102 Element verb = digVerb.getDocumentElement(); 104 if (!verb.hasAttribute( DIGProfile.URI )) { 105 verb.setAttribute( DIGProfile.URI, m_kbURI ); 106 } 107 108 URL url = new URL( m_extReasonerURL ); 110 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 111 112 StringWriter out = new StringWriter(); 114 serialiseDocument( digVerb, out ); 115 116 conn.setDoOutput( true ); 117 conn.setRequestMethod( "POST" ); 118 conn.setRequestProperty( "Content-Length", Integer.toString( out.getBuffer().length() ) ); 119 120 conn.setRequestProperty( "Content-Type", profile.getContentType() ); 122 123 logMessage( true, digVerb ); 125 126 conn.connect(); 128 PrintWriter pw = FileUtils.asPrintWriterUTF8( conn.getOutputStream() ); 129 pw.print( out.getBuffer() ); 130 pw.flush(); 131 pw.close(); 132 133 Document response = getDigResponse( conn ); 135 136 logMessage( false, response ); 138 139 errorCheck( response ); 140 return response; 141 } 142 catch (IOException e) { 143 throw new DIGWrappedException( e ); 144 } 145 } 146 147 148 153 public void serialiseDocument( Document doc, Writer out ) { 154 try { 155 XMLSerializer serializer = new XMLSerializer ( out, createXMLFormatter( doc ) ); 157 serializer.asDOMSerializer(); 158 serializer.serialize( doc ); 159 } 160 catch (IOException e) { 161 throw new DIGWrappedException( e ); 162 } 163 } 164 165 166 172 public void bindKB( boolean rebind, DIGProfile profile ) { 173 if (rebind && m_kbURI != null) { 175 Document release = createDigVerb( DIGProfile.RELEASEKB, profile ); 176 177 Document response = sendDigVerb( release, profile ); 178 errorCheck( response ); 179 180 if (warningCheck(response)) { 181 log.warn( "DIG reasoner warning: " + getWarnings().next() ); 182 } 183 m_kbURI = null; 184 } 185 186 if (m_kbURI == null) { 188 Document response = sendDigVerb( createDigVerb( DIGProfile.NEWKB, profile ), profile ); 190 errorCheck( response ); 191 192 Element kb = (Element) response.getDocumentElement() 194 .getElementsByTagName( DIGProfile.KB ) 195 .item( 0 ); 196 if (kb == null) { 197 throw new DIGReasonerException( "Could not locate DIG KB identifier in return value from newKB" ); 198 } 199 else { 200 m_kbURI = kb.getAttribute( DIGProfile.URI ); 201 } 202 } 203 } 204 205 206 211 public void errorCheck( Document response ) { 212 Element root = response.getDocumentElement(); 213 NodeList errs = root.getElementsByTagName( DIGProfile.ERROR ); 214 215 if (errs != null && errs.getLength() > 0) { 216 Element error = (Element) errs.item( 0 ); 217 218 String errCode = error.getAttribute( DIGProfile.CODE ); 219 int code = (errCode == null || errCode.length() == 0) ? 0 : Integer.parseInt( errCode ); 220 221 String msgAttr = error.getAttribute( DIGProfile.MESSAGE ); 222 223 NodeList messages = error.getChildNodes(); 224 String message = (messages.getLength() > 0) ? ((Text) messages.item( 0 )).getNodeValue().trim() : "(no message)"; 225 226 throw new DIGErrorResponseException( message, msgAttr, code ); 227 } 228 } 229 230 231 237 public boolean warningCheck( Document response ) { 238 Element root = response.getDocumentElement(); 239 NodeList ok = root.getElementsByTagName( DIGProfile.OK ); 240 m_warnings.clear(); 241 242 if (ok != null && ok.getLength() > 0) { 243 Element e = (Element) ok.item(0); 244 NodeList warnings = e.getElementsByTagName( DIGProfile.WARNING ); 245 246 if (warnings != null && warnings.getLength() > 0) { 247 for (int i = 0; i < warnings.getLength(); i++) { 248 Element warn = (Element) warnings.item( i ); 250 m_warnings.add( warn.getAttribute( DIGProfile.MESSAGE ) ); 251 } 252 253 return true; 254 } 255 } 256 257 return false; 258 } 259 260 261 265 public Iterator getWarnings() { 266 return m_warnings.iterator(); 267 } 268 269 270 273 public void release() { 274 DIGConnectionPool.getInstance().release( this ); 275 } 276 277 278 282 public String getReasonerURL() { 283 return m_extReasonerURL; 284 } 285 286 287 291 public void setReasonerURL( String url ) { 292 m_extReasonerURL = url; 293 m_kbURI = null; 294 } 295 296 297 300 307 protected Document getDigResponse( HttpURLConnection conn ) { 308 try { 309 if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { 311 throw new DIGReasonerException( "DIG reasoner returned failure code " + 312 conn.getResponseCode() + ": " + 313 conn.getResponseMessage() ); 314 } 315 316 BufferedInputStream in = new BufferedInputStream( conn.getInputStream() ); 317 StringBuffer buf = new StringBuffer (); 318 319 int ch = in.read(); 321 while (ch > 0) { 322 buf.append( (char) ch ); 323 ch = in.read(); 324 } 325 326 DocumentBuilder builder = m_factory.newDocumentBuilder(); 329 return builder.parse( new ByteArrayInputStream( buf.toString().getBytes() ) ); 330 } 331 catch (Exception e) { 332 e.printStackTrace( System.err ); 333 throw new DIGWrappedException( e ); 334 } 335 } 336 337 338 343 protected OutputFormat createXMLFormatter( Document doc ) { 344 OutputFormat format = new OutputFormat( doc ); 345 format.setIndenting( true ); 346 format.setLineWidth( 0 ); 347 format.setPreserveSpace( false ); 348 349 return format; 350 } 351 352 353 358 protected Document createDigVerb( String verbName, DIGProfile profile ) { 359 try { 360 DocumentBuilder builder = m_factory.newDocumentBuilder(); 362 Document doc = builder.newDocument(); 363 364 Element root = doc.createElementNS( profile.getDIGNamespace(), verbName ); 366 doc.appendChild( root ); 367 368 root.setAttribute( "xmlns", profile.getDIGNamespace() ); 370 root.setAttribute( "xmlns:xsi", XSI ); 371 root.setAttributeNS( XSI, "xsi:schemaLocation", 372 profile.getDIGNamespace() + " " + profile.getSchemaLocation() ); 373 if (m_kbURI != null) { 374 root.setAttribute( DIGProfile.URI, m_kbURI ); 375 } 376 377 return doc; 378 } 379 catch (FactoryConfigurationError e) { 380 throw new DIGWrappedException( e ); 381 } 382 catch (ParserConfigurationException e) { 383 throw new DIGWrappedException( e ); 384 } 385 } 386 387 388 393 protected void logMessage( boolean outgoing, Document msg ) { 394 if (m_logCommunications) { 395 StringWriter out = new StringWriter(); 396 serialiseDocument( msg, out ); 397 398 if (log.isDebugEnabled()) { 399 log.debug( outgoing ? "Sending to DIG reasoner ..." : "Received from DIG reasoner ..." ); 400 log.debug( out ); 401 } 402 } 403 } 404 405 406 410 } 411 412 413 439 | Popular Tags |