From 6930074afc090675a934131c6a77f4569709c4d0 Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Tue, 11 Jul 2017 15:46:03 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngine@150996 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../graphtools/utils/DateGuesser.java | 27 ++------ .../graphtools/utils/HttpRequest.java | 65 +++++++------------ 2 files changed, 30 insertions(+), 62 deletions(-) diff --git a/src/main/java/org/gcube/contentmanagement/graphtools/utils/DateGuesser.java b/src/main/java/org/gcube/contentmanagement/graphtools/utils/DateGuesser.java index 9bb1b38..900ffed 100644 --- a/src/main/java/org/gcube/contentmanagement/graphtools/utils/DateGuesser.java +++ b/src/main/java/org/gcube/contentmanagement/graphtools/utils/DateGuesser.java @@ -7,28 +7,12 @@ import java.util.Calendar; import java.util.Date; import java.util.Locale; -import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DateGuesser { - - public static void main(String[] args) { - Calendar c = convertDate("20/2/2010 0:49:00"); - System.out.println("giorno " + c.get(Calendar.DAY_OF_MONTH) + " mese " + (c.get(Calendar.MONTH) + 1) + " anno " + c.get(Calendar.YEAR)); - System.out.println("pattern " + getPattern("20/2/2010 0:49:00")); - System.out.println("pattern " + getPattern("2009-05-12 15:42:10")); - System.out.println("pattern " + getPattern("1970")); - System.out.println("pattern " + getPattern("2009-05-12")); - System.out.println("pattern " + getPattern("prova")); - System.out.println("pattern " + getPattern("9/26/2010 1:49:00")); - System.out.println("pattern " + getPattern("0.99")); - System.out.println("pattern " + getPattern("01/25/2013 14:48")); - c = convertDate("2009-05-12"); - System.out.println("giorno " + c.get(Calendar.DAY_OF_MONTH) + " mese " + (c.get(Calendar.MONTH) + 1) + " anno " + c.get(Calendar.YEAR)); - // Properties prop = new Properties(System.getProperties()); - - // prop.list(System.out); - - } + + private static Logger LOGGER = LoggerFactory.getLogger(DateGuesser.class); public static Calendar convertDate(String data) { return convertDate(data, null); @@ -46,6 +30,7 @@ public class DateGuesser { if (isSeconds(data)) return "s"; + // String language = System.getProperty("user.language"); Locale l = Locale.UK; if (language != null && language.equalsIgnoreCase("it")) @@ -90,7 +75,7 @@ public class DateGuesser { if ((("" + secondsint).length() == 4) && (secondsint == seconds)) isSeconds = false; else { - AnalysisLogger.getLogger().debug("This entry contains seconds indication"); + LOGGER.debug("This entry contains seconds indication"); isSeconds = true; } } catch (Exception e1) { diff --git a/src/main/java/org/gcube/contentmanagement/graphtools/utils/HttpRequest.java b/src/main/java/org/gcube/contentmanagement/graphtools/utils/HttpRequest.java index dcc051e..d5e65d0 100644 --- a/src/main/java/org/gcube/contentmanagement/graphtools/utils/HttpRequest.java +++ b/src/main/java/org/gcube/contentmanagement/graphtools/utils/HttpRequest.java @@ -30,16 +30,15 @@ import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; -import org.apache.commons.httpclient.methods.multipart.FilePart; -import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; -import org.apache.commons.httpclient.methods.multipart.Part; -import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; -import org.gcube.contentmanagement.lexicalmatcher.utils.FileTools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.google.gson.Gson; public class HttpRequest { + + private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequest.class); + /** * Sends an HTTP GET request to a url * @@ -74,7 +73,7 @@ public class HttpRequest { rd.close(); result = sb.toString(); } catch (Exception e) { - e.printStackTrace(); + LOGGER.error("error sending get request",e); } } return result; @@ -137,7 +136,7 @@ public class HttpRequest { public static Object getJSonData(String endpoint, String requestParameters, Type outputClass) throws Exception { String output = sendGetRequest(endpoint, requestParameters); Gson gson = new Gson(); - // AnalysisLogger.getLogger().debug("HttpRequest-> OUTPUT JSON:\n"+output.toString()); + // LOGGER.debug("HttpRequest-> OUTPUT JSON:\n"+output.toString()); // Output the response Object rebuiltJson = gson.fromJson(output.toString(), outputClass); return rebuiltJson; @@ -176,7 +175,7 @@ public class HttpRequest { writer = new OutputStreamWriter(out); // write parameters String jsonString = gson.toJson(obj); - AnalysisLogger.getLogger().trace("INPUT JSON:\n" + jsonString); + LOGGER.trace("INPUT JSON:\n" + jsonString); writer.write(jsonString); writer.flush(); } @@ -193,13 +192,13 @@ public class HttpRequest { writer.close(); reader.close(); - // AnalysisLogger.getLogger().debug("OUTPUT JSON:\n"+answer.toString()); + // LOGGER.debug("OUTPUT JSON:\n"+answer.toString()); // Output the response Object rebuiltJson = gson.fromJson(answer.toString(), outputClass); return rebuiltJson; } catch (Exception ex) { - ex.printStackTrace(); + LOGGER.error("error executing http call",ex); } return null; } @@ -247,9 +246,9 @@ public class HttpRequest { return answer.toString(); } catch (MalformedURLException ex) { - ex.printStackTrace(); + LOGGER.error("error sending post request",ex); } catch (IOException ex) { - ex.printStackTrace(); + LOGGER.error("error sending post request",ex); } return null; } @@ -284,7 +283,7 @@ public class HttpRequest { checkConn = conn.getResponseCode(); conn.disconnect(); } catch (Exception e) { - System.out.println("ERROR in URL " + e.getMessage()); + LOGGER.error("error in url",e); } return checkConn; } @@ -293,7 +292,7 @@ public class HttpRequest { URL smpFile = new URL(fileurl); URLConnection uc = (URLConnection) smpFile.openConnection(); InputStream is = uc.getInputStream(); - AnalysisLogger.getLogger().debug("GenericWorker-> Retrieving from " + fileurl + " to :" + localFile); + LOGGER.debug("GenericWorker-> Retrieving from " + fileurl + " to :" + localFile); inputStreamToFile(is, localFile); is.close(); is = null; @@ -328,10 +327,15 @@ public class HttpRequest { } - int status = client.executeMethod(postMethod); - - byte[] responseBody = postMethod.getResponseBody(); - String response = new String(responseBody); + client.executeMethod(postMethod); + + String response = ""; + try(BufferedReader bis = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream()))){ + String line = null; + while ((line = bis.readLine())!=null) + response += line ; + } + return response; } @@ -352,7 +356,7 @@ public class HttpRequest { } - int status = client.executeMethod(postMethod); + client.executeMethod(postMethod); byte[] responseBody = postMethod.getResponseBody(); String response = new String(responseBody); @@ -360,25 +364,4 @@ public class HttpRequest { return response; } - public static void main(String[] args) throws Exception { - /* - * String url = "http://geoserver-dev.d4science-ii.research-infrastructures.eu/geoserver/rest/layergroups/group4402c0cff-27e3-4606-a2f1-993ad37c3dfb.json"; int d = checkUrl(url, "admin", "gcube@geo2010"); System.out.println(d); - */ - String host = "dataminer1-devnext.d4science.org"; - int port = 80; - String url = "http://" + host + ":" + port + "/wps/WebProcessingService"; - - String username = "gianpaolo.coro"; - String password = "cb289202-e7d6-45ee-8076-a80bc4d4be51|98187548"; - String parameters = ""; - File filetosend = new File("dbscan_req_2016-07-05_01_49_01.507.xml"); - /* - String fileloaded = FileTools.loadString(filetosend.getAbsolutePath(), "UTF-8"); - fileloaded = fileloaded.trim().replaceFirst("^([\\W]+)<","<"); - File testfile = new File("test.xml"); - FileTools.saveString(testfile.getAbsolutePath(), fileloaded, true, "UTF-8"); - */ - // PostData(url, parameters, username, password, filetosend); - PostFileData(url, host, port, new LinkedHashMap(), username, password, filetosend); - } } \ No newline at end of file