From 97a6a3f19b5b18cdb979fb10a1cc8a45ac422073 Mon Sep 17 00:00:00 2001 From: nicolas Date: Wed, 13 Jun 2018 14:31:30 +0200 Subject: [PATCH] Modification in PreviewThread and PreviewManager classes to handle query to HTTPS nodes --- .../portal/entity/query/HttpHeadResponse.java | 2 +- .../session/preview/PreviewManager.java | 140 ++++++++++-------- .../portal/session/preview/PreviewThread.java | 52 +++++-- .../org/vamdc/portal/species/NodeSpecies.java | 13 +- 4 files changed, 131 insertions(+), 76 deletions(-) diff --git a/portal.ejb/src/main/java/org/vamdc/portal/entity/query/HttpHeadResponse.java b/portal.ejb/src/main/java/org/vamdc/portal/entity/query/HttpHeadResponse.java index 130b9eb..ea1e98b 100644 --- a/portal.ejb/src/main/java/org/vamdc/portal/entity/query/HttpHeadResponse.java +++ b/portal.ejb/src/main/java/org/vamdc/portal/entity/query/HttpHeadResponse.java @@ -61,7 +61,7 @@ public HttpHeadResponse(){ public HttpHeadResponse(String ivoaID, HttpURLConnection connection) { this.ivoaID = ivoaID; this.fullQueryURL = connection.getURL().toString(); - this.status = retrieveStatus(connection); + this.status = retrieveStatus(connection); retrieveHeaders(connection); if (truncated>0 && truncated<100) this.status=Response.TRUNCATED; diff --git a/portal.ejb/src/main/java/org/vamdc/portal/session/preview/PreviewManager.java b/portal.ejb/src/main/java/org/vamdc/portal/session/preview/PreviewManager.java index 2c57992..aae7079 100644 --- a/portal.ejb/src/main/java/org/vamdc/portal/session/preview/PreviewManager.java +++ b/portal.ejb/src/main/java/org/vamdc/portal/session/preview/PreviewManager.java @@ -28,28 +28,32 @@ @Name("preview") @Scope(ScopeType.PAGE) -public class PreviewManager implements Serializable{ +public class PreviewManager implements Serializable { private static final long serialVersionUID = 2029452631857959114L; @Logger Log log; - @In transient QueryData queryData; + @In + transient QueryData queryData; + + @In(create = true) + transient RegistryFacade registryFacade; + + @In(create = true) + transient ConsumerRequestRegistry consumers; + + //private volatile boolean completeEventCalled = false; - @In(create=true) transient RegistryFacade registryFacade; - - @In(create=true) transient ConsumerRequestRegistry consumers; - - private volatile boolean completeEventCalled=false; - private Collection> nodeFutureResponses = new ArrayList>(); private long startTime; - + private long percentDone; - - public void initiate(){ - + private Boolean securedProtocolsAreSet = false; + + public void initiate() { + this.setSSLProtocols(); if (!nodeFutureResponses.isEmpty()) return; @@ -57,25 +61,28 @@ public void initiate(){ if (activeNodes.isEmpty()) return; - - ExecutorService executor = Executors.newFixedThreadPool(activeNodes.size()); - for (String ivoaID:activeNodes){ - try{ - nodeFutureResponses.add(executor.submit(new PreviewThread(ivoaID,registryFacade.getVamdcTapMirrors(ivoaID),queryData.getQueryString()))); - }catch (IllegalArgumentException e){ + ExecutorService executor = Executors.newFixedThreadPool(activeNodes + .size()); + + for (String ivoaID : activeNodes) { + try { + nodeFutureResponses.add(executor.submit(new PreviewThread( + ivoaID, registryFacade.getVamdcTapMirrors(ivoaID), + queryData.getQueryString()))); + } catch (IllegalArgumentException e) { } } - + executor.shutdown(); startTime = new Date().getTime(); } - - public List getNodes(){ - TreeSet nodes = new TreeSet(new HttpHeadResponseComparator()); - for (Future task:nodeFutureResponses){ - if (task.isDone()&& !task.isCancelled()){ + public List getNodes() { + TreeSet nodes = new TreeSet( + new HttpHeadResponseComparator()); + for (Future task : nodeFutureResponses) { + if (task.isDone() && !task.isCancelled()) { try { HttpHeadResponse response = task.get(); nodes.add(response); @@ -91,78 +98,91 @@ public List getNodes(){ return new ArrayList(nodes); } - private class HttpHeadResponseComparator implements Comparator{ + private class HttpHeadResponseComparator implements + Comparator { @Override public int compare(HttpHeadResponse o1, HttpHeadResponse o2) { - if (o1==null || o2==null) + if (o1 == null || o2 == null) return 0; Integer value1 = Integer.valueOf(o1.getStatus().ordinal()); Integer value2 = Integer.valueOf(o2.getStatus().ordinal()); int compare = value1.compareTo(value2); - if (compare!=0) - return compare; - else if(o1.getProcesses()!=o2.getProcesses()) - return o2.getProcesses()-o1.getProcesses(); - else - return o1.getIvoaID().compareTo(o2.getIvoaID()); - + if (compare != 0) + return compare; + else if (o1.getProcesses() != o2.getProcesses()) + return o2.getProcesses() - o1.getProcesses(); + else + return o1.getIvoaID().compareTo(o2.getIvoaID()); + } - - } - - public boolean isDone(){ - if(this.percentDone <= 100){ - for (Future task:nodeFutureResponses){ - if (!task.isDone()){ + public boolean isDone() { + if (this.percentDone <= 100) { + for (Future task : nodeFutureResponses) { + if (!task.isDone()) { return false; } } - } - + } + return true; } - - public Long getPercentsDone(){ + + public Long getPercentsDone() { Long result; - if (isDone()){ - result=101L; - } else{ + if (isDone()) { + result = 101L; + } else { Long now = new Date().getTime(); - result = (100L*(now-startTime)/Settings.HTTP_HEAD_TIMEOUT.getInt()); + result = (100L * (now - startTime) / Settings.HTTP_HEAD_TIMEOUT + .getInt()); } this.percentDone = result; return result; } - public String getStringStatus(){ + public String getStringStatus() { if (isDone()) return "Done"; - return ""+getNodes().size()+" nodes of "+nodeFutureResponses.size()+" responded"; + return "" + getNodes().size() + " nodes of " + + nodeFutureResponses.size() + " responded"; } - - public void cancel(){ - for (Future task:nodeFutureResponses){ + + public void cancel() { + for (Future task : nodeFutureResponses) { if (!task.isDone()) task.cancel(true); } } - - public void clear(){ + + public void clear() { cancel(); - nodeFutureResponses=new ArrayList>(); + nodeFutureResponses = new ArrayList>(); } - - public String refine(){ + + public String refine() { clear(); - if(!queryData.isGuidedQuery()) + if (!queryData.isGuidedQuery()) return RedirectPage.QUERY; else return RedirectPage.QUERYTREE; } - + /** + * Set SSL protocols to be used. + * Fix a bug found when switching from java 7 to 8 : + * If this list is not set, the HTTPS request failed with java 8 but not + * with java 7 + * + */ + private void setSSLProtocols() { + if (!securedProtocolsAreSet) { + System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); + this.securedProtocolsAreSet = true; + } + } + } diff --git a/portal.ejb/src/main/java/org/vamdc/portal/session/preview/PreviewThread.java b/portal.ejb/src/main/java/org/vamdc/portal/session/preview/PreviewThread.java index 3da24f5..7069a02 100644 --- a/portal.ejb/src/main/java/org/vamdc/portal/session/preview/PreviewThread.java +++ b/portal.ejb/src/main/java/org/vamdc/portal/session/preview/PreviewThread.java @@ -8,47 +8,71 @@ import java.util.Collection; import java.util.concurrent.Callable; +import javax.net.ssl.HttpsURLConnection; + +import org.jboss.seam.annotations.Logger; +import org.jboss.seam.log.Log; import org.vamdc.portal.Settings; import org.vamdc.portal.entity.query.HttpHeadResponse; +public class PreviewThread implements Callable { + -public class PreviewThread implements Callable{ + @Logger + Log log; private String ivoaID; private String query; private Collection mirrors; - - public PreviewThread(String ivoaID, Collection mirrors, String query){ + + public PreviewThread(String ivoaID, Collection mirrors, String query) { this.ivoaID = ivoaID; this.query = query; - this.mirrors = mirrors; + this.mirrors = mirrors; } - + private URL getQuery(URL mirror) { - URL result=null; + URL result = null; try { - result = new URL(mirror+"sync?LANG=VSS2&REQUEST=doQuery&FORMAT=XSAMS&QUERY="+URLEncoder.encode(query,"UTF-8")); + result = new URL(mirror + + "sync?LANG=VSS2&REQUEST=doQuery&FORMAT=XSAMS&QUERY=" + + URLEncoder.encode(query, "UTF-8")); } catch (MalformedURLException e) { + log.info(e); } catch (UnsupportedEncodingException e) { + log.info(e); } return result; } - + + /** + * Send a HEAD request to a node by testing its mirrors + */ @Override public HttpHeadResponse call() throws Exception { - HttpHeadResponse response =null; - for (URL mirror:mirrors){ + HttpHeadResponse response = null; + for (URL mirror : mirrors) { + HttpURLConnection connection = null; URL queryURL = getQuery(mirror); - HttpURLConnection connection = (HttpURLConnection) queryURL.openConnection(); - + + //http request + if (!mirror.getProtocol().equals("https")) { + connection = (HttpURLConnection) queryURL.openConnection(); + } + //https request + else { + connection = (HttpsURLConnection) queryURL.openConnection(); + } + connection.setRequestMethod("HEAD"); connection.setReadTimeout(Settings.HTTP_HEAD_TIMEOUT.getInt()); - - response = new HttpHeadResponse(ivoaID,connection); + + response = new HttpHeadResponse(ivoaID, connection); if (response.isOk()) return response; } + return response; } diff --git a/portal.ejb/src/main/java/org/vamdc/portal/species/NodeSpecies.java b/portal.ejb/src/main/java/org/vamdc/portal/species/NodeSpecies.java index 5776362..ec734e5 100644 --- a/portal.ejb/src/main/java/org/vamdc/portal/species/NodeSpecies.java +++ b/portal.ejb/src/main/java/org/vamdc/portal/species/NodeSpecies.java @@ -68,14 +68,20 @@ public void querySpecies(String ivoaId, SpeciesResult resultBean){ resultBean.setReady(true); return; } - + System.out.println("### test "); resultBean.setMirrorCount(mirrors.size()); for(URL node:mirrors){ resultBean.setMessage("Timeout : query execution was too long."); resultBean.nextMirror(); URL queryUrl=node; + System.out.println("query"); + System.out.println(query); + System.out.println(queryUrl); try { queryUrl = new URL(node.toExternalForm()+query); + + System.out.print("queryUrl"); + System.out.print(queryUrl); resultBean.setFormattedResult( formatRequestResult(queryUrl)); resultBean.setMessage(""); resultBean.setReady(true); @@ -85,6 +91,8 @@ public void querySpecies(String ivoaId, SpeciesResult resultBean){ resultBean.setMessage("Incorrect service URL "+queryUrl); } catch (IOException e) { // TODO Auto-generated catch block + + e.printStackTrace(); resultBean.setMessage("Error while proceeding request"); } catch (TransformerException e) { // TODO Auto-generated catch block @@ -110,7 +118,10 @@ public void querySpecies(String ivoaId, SpeciesResult resultBean){ * @throws TransformerException */ private static String formatRequestResult(URL url) throws IOException, TransformerException, PortalHttpException{ + System.out.print("###"); + System.out.print(url); HttpURLConnection c = (HttpURLConnection)url.openConnection(); + if(c.getResponseCode() == 200){ c.setConnectTimeout(Settings.HTTP_CONNECT_TIMEOUT.getInt()); c.setReadTimeout(Settings.HTTP_DATA_TIMEOUT.getInt());