001 // This file is part of the Attempto Java Packages. 002 // Copyright 2008, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch). 003 // 004 // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the 005 // terms of the GNU Lesser General Public License as published by the Free Software Foundation, 006 // either version 3 of the License, or (at your option) any later version. 007 // 008 // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY 009 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 010 // PURPOSE. See the GNU Lesser General Public License for more details. 011 // 012 // You should have received a copy of the GNU Lesser General Public License along with the Attempto 013 // Java Packages. If not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.ape; 016 017 import java.io.BufferedReader; 018 import java.io.IOException; 019 import java.io.InputStream; 020 import java.io.InputStreamReader; 021 022 import org.apache.commons.httpclient.HttpClient; 023 import org.apache.commons.httpclient.HttpStatus; 024 import org.apache.commons.httpclient.NameValuePair; 025 import org.apache.commons.httpclient.methods.PostMethod; 026 027 /** 028 * This class provides an interface to the Attempto Parsing Engine (APE) webservice 029 * (i.e. HTTP server). 030 * The HTTP server implementation is provided by <code>ape.exe</code>. To start 031 * a server, execute for example: 032 * 033 * <pre> 034 * ./ape.exe -httpserver -port 8000 035 * </pre> 036 * 037 * @author Kaarel Kaljurand 038 * @author Tobias Kuhn 039 * 040 * TODO Why don't we use GET instead of POST (as least for shorter queries)? 041 */ 042 public class APEWebservice extends ACEParser { 043 044 private String wsUrl; 045 private NameValuePair uri = new NameValuePair("uri", ""); 046 private NameValuePair text = new NameValuePair("text", ""); 047 private NameValuePair ulextext = new NameValuePair("ulextext", ""); 048 private NameValuePair solo = new NameValuePair("solo", "owlxml"); 049 private NameValuePair guess = new NameValuePair("guess", "off"); 050 private NameValuePair noclex = new NameValuePair("noclex", "off"); 051 052 /** 053 * Creates a new parser object based on the URL of a running APE webservice. 054 * 055 * @param wsUrl The URL of the APE webservice. 056 */ 057 public APEWebservice(String wsUrl) { 058 this.wsUrl = wsUrl; 059 } 060 061 062 public String getSoloOutput(String aceText, Lexicon lexicon, OutputType outputType) throws ACEParserException { 063 setText(aceText); 064 setUlex(lexicon); 065 setSolo(outputType); 066 return checkForErrors(getParserResponseAsString(new NameValuePair[] {text, ulextext, solo, uri, guess, noclex})); 067 } 068 069 070 public ACEParserResult getMultiOutput(String aceText, Lexicon lexicon, OutputType... outputTypes) { 071 NameValuePair[] params = new NameValuePair[outputTypes.length + 5]; 072 setText(aceText); 073 setUlex(lexicon); 074 params[0] = text; 075 params[1] = ulextext; 076 params[2] = uri; 077 params[3] = guess; 078 params[4] = noclex; 079 int counter = 5; 080 for (OutputType t : outputTypes) { 081 params[counter] = new NameValuePair(t.toMultiFlag(), "on"); 082 counter++; 083 } 084 return new ACEParserResult(getParserResponseAsString(params)); 085 } 086 087 088 public void setURI(String uri) { 089 super.setURI(uri); 090 this.uri.setValue(getURI()); 091 } 092 093 094 public void setGuessingEnabled(boolean guessing) { 095 super.setGuessingEnabled(guessing); 096 if (isGuessingEnabled()) { 097 this.guess.setValue("on"); 098 } 099 else { 100 this.guess.setValue("off"); 101 } 102 } 103 104 105 public void setClexEnabled(boolean clexEnabled) { 106 super.setClexEnabled(clexEnabled); 107 108 if (isClexEnabled()) { 109 this.noclex.setValue("off"); 110 } 111 else { 112 this.noclex.setValue("on"); 113 } 114 } 115 116 117 private void setUlex(Lexicon lexicon) { 118 if (lexicon == null) { 119 this.ulextext.setValue(""); 120 } 121 else { 122 this.ulextext.setValue(lexicon.toString()); 123 } 124 } 125 126 private void setText(String text) { 127 this.text.setValue(text); 128 } 129 130 131 private void setSolo(OutputType solo) { 132 this.solo.setValue(solo.toSoloFlag()); 133 } 134 135 private String getParserResponseAsString(NameValuePair[] parameters) { 136 HttpClient client = new HttpClient(); 137 PostMethod method = new PostMethod(wsUrl); 138 method.setRequestBody(parameters); 139 String response = null; 140 141 try { 142 int statusCode = client.executeMethod(method); 143 144 if (statusCode == HttpStatus.SC_OK) { 145 response = inputStreamAsString(method.getResponseBodyAsStream()); 146 } 147 else { 148 throw new RuntimeException("HTTP request failed: " + method.getStatusLine() + ": " + wsUrl); 149 } 150 } catch (Exception e) { 151 throw new RuntimeException("Accessing APE webservice failed: " + e.getMessage()); 152 } finally { 153 method.releaseConnection(); 154 } 155 156 return response; 157 } 158 159 private String inputStreamAsString(InputStream stream) throws IOException { 160 BufferedReader br = new BufferedReader(new InputStreamReader(stream)); 161 StringBuffer sb = new StringBuffer(); 162 String line = null; 163 164 while ((line = br.readLine()) != null) { 165 sb.append(line); 166 sb.append('\n'); 167 } 168 169 br.close(); 170 return sb.toString(); 171 } 172 }