Java tutorial
/* * Copyright (C) 2016 Behrang QasemiZadeh <zadeh at phil.hhu.de> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package ie.pars.nlp.sketchengine.interactions; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.URI; import org.apache.commons.httpclient.URIException; import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.json.JSONObject; //import org.json.simple.parser.JSONParser; /** * * @author bq */ public abstract class SKEInteractionsBase implements Runnable { //final File outputFile; private final String rootURL; final String corpus; final String query; public SKEInteractionsBase(String rootURL, String corpus, String query) { this.rootURL = rootURL; this.corpus = corpus; //this.outputFile = outputFile; this.query = query; // this.rootURL = "corpora.phil.hhu.de"; } /** * Execute the query and return the results a json Object produced by SKE * * @param client * @param query * @return * @throws URIException * @throws IOException */ JSONObject getHTTP(HttpClient client, String query) throws URIException, IOException { GetMethod getURI = new GetMethod(new URI(query, true).toString()); client.executeMethod(getURI); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getURI.getResponseBodyAsStream())); String line; StringBuilder k = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { // System.out.println(line); k.append(line).append("\n"); } JSONObject json = new JSONObject(k.toString()); return json; } /** * Create a session id to communicate with the server * * @return */ protected HttpClient getSessionID() { String cookie_policy = CookiePolicy.DEFAULT; //use CookiePolicy.BROWSER_COMPATIBILITY in case cookie handling does not work HttpClient client = new HttpClient(); try { Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), 443)); client.getHostConfiguration().setHost(rootURL, 80, "http"); client.getParams().setCookiePolicy(cookie_policy); } catch (java.security.GeneralSecurityException | IOException e) { e.printStackTrace(); } client.getParams().setCookiePolicy(cookie_policy); // retrieve session id GetMethod authget = new GetMethod("/login/"); try { int code = client.executeMethod(authget); } catch (IOException ex) { System.err.println("Error: couldn't retrieve session ID from Sketch Engine server."); System.exit(1); } authget.releaseConnection(); return client; } public String getRootURL() { return rootURL; } }