Skip to content

Commit

Permalink
Merge pull request #90 from nicolasmoreau/master
Browse files Browse the repository at this point in the history
Fix for https request
  • Loading branch information
nicolasmoreau authored May 18, 2022
2 parents 05bf58f + 2f0dc1a commit 01698a2
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 70 deletions.
3 changes: 3 additions & 0 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ Could not instantiate Seam component: registration
Le fichier ~/.mavenrc a été édité pour que JAVA_HOME pointe sur java7
JAVA_HOME est défini dans deploy.sh pour que jboss utilise java7

Autre solution : export JAVA_HOME=~/Applications/jdk1.8.0_152/


Utilise JSF en version 1.x

16 changes: 12 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<repository>
<id>vamdc repository</id>
<name>VAMDC repository</name>
<url>http://dev.vamdc.org/nexus/content/groups/public/</url>
<url>http://nexus.vamdc.org/nexus/content/groups/public/</url>
<layout>default</layout>
</repository>
</repositories>
Expand All @@ -50,17 +50,25 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<!--<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<argLine>-Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
</configuration>
</plugin>
</plugin>-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
</plugin>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
22 changes: 19 additions & 3 deletions portal.ear/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
<groupId>org.jboss.el</groupId>
<artifactId>jboss-el</artifactId>
</dependency>
<dependency>
<!-- <dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
</dependency>
<version>6.0.1.Final</version>
</dependency>-->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
Expand All @@ -48,6 +49,21 @@
<artifactId>el-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.drools/drools-core -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>5.4.0.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.drools/drools-compiler -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>5.4.0.Final</version>
</dependency>


</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -114,4 +130,4 @@
</dependency>
</dependencies>
</dependencyManagement>
</project>
</project>
6 changes: 3 additions & 3 deletions portal.ejb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<version>20220320</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -175,4 +175,4 @@
</dependency>
</dependencies>
</dependencyManagement>
</project>
</project>
1 change: 1 addition & 0 deletions portal.ejb/src/main/java/org/vamdc/portal/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
public enum Settings {

//superseded by values in server/default/deploy/properties-service.xml of jboss server
HTTP_CONNECT_TIMEOUT("HTTPConnTimeout", "2000"),
HTTP_DATA_TIMEOUT("HTTPDataTimeout","30000"),
REGISTRY_URL("registryURL","http://registry.vamdc.eu/registry-12.07/services/RegistryQueryv1_0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.vamdc.portal.entity.constant.Species;

Expand Down Expand Up @@ -40,13 +41,14 @@ static Collection<String> suggestMoleculeName(EntityManager em,
@SuppressWarnings("unchecked")
private static Collection<String> suggestRestrictedSpeciesName(EntityManager em,
String name, Integer speciesType) {
return em.createQuery("SELECT DISTINCT vsn.name from VamdcSpeciesNames vsn JOIN vsn.vamdcSpecies vsp " +
Query q = em.createQuery("SELECT DISTINCT vsn.name from VamdcSpeciesNames vsn JOIN vsn.vamdcSpecies vsp " +
"WHERE vsn.name LIKE :speciesName and vsn.vamdcMarkupTypes.id=1 and vsp.speciesType=:speciesType "
+ " order by length(vsn.name), vsn.searchPriority")
.setParameter("speciesName", "%"+name+"%")
.setParameter("speciesType", speciesType)
.setMaxResults(20)
.getResultList();
.setMaxResults(20);
return q.getResultList();


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ private void initNodeConsumers() {

// recommended consumers
for (String ivoaID : visibleConsumers) {
consumers.add(new SelectItem(ivoaID, " ** "
+ registryFacade.getResourceTitle(ivoaID)));
consumers.add(new SelectItem(ivoaID, registryFacade.getResourceTitle(ivoaID)));
}

// other consumers
for (String ivoaID : registryFacade.getConsumerIvoaIDs()) {
if (visibleConsumers.contains(ivoaID) == false) {
consumers.add(new SelectItem(ivoaID, registryFacade
if (!visibleConsumers.contains(ivoaID)) {
consumers.add(new SelectItem(ivoaID, "<Not recommended> " + registryFacade
.getResourceTitle(ivoaID)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,55 @@

import org.vamdc.portal.Settings;



public class PostRequest implements Callable<URL>{
public class PostRequest implements Callable<URL> {

private URL consumer;
private Collection<URL> nodes;
public PostRequest(URL consumer, Collection<URL> nodes){

public PostRequest(URL consumer, Collection<URL> nodes) {
this.consumer = consumer;
this.nodes = nodes;
}

@Override
public URL call() throws Exception {

HttpURLConnection connection = setupConnection();

return processResponse(connection);
}

private URL processResponse(HttpURLConnection connection)
throws IOException, MalformedURLException {
URL result=null;
private URL processResponse(HttpURLConnection connection) throws IOException, MalformedURLException {
URL result = null;
int resultCode = connection.getResponseCode();

if (resultCode== HttpURLConnection.HTTP_MOVED_TEMP || resultCode== HttpURLConnection.HTTP_SEE_OTHER || resultCode==HttpURLConnection.HTTP_MOVED_PERM){
if (resultCode == HttpURLConnection.HTTP_MOVED_TEMP || resultCode == HttpURLConnection.HTTP_SEE_OTHER
|| resultCode == HttpURLConnection.HTTP_MOVED_PERM) {
result = new URL(connection.getHeaderField("Location"));
}else{
throw new IOException("XSAMS processor service returned "+resultCode+" status code.");
} else {
throw new IOException("XSAMS processor service returned " + resultCode + " status code.");
}
return result;
}

private HttpURLConnection setupConnection() throws IOException,
ProtocolException {
private HttpURLConnection setupConnection() throws IOException, ProtocolException {
HttpURLConnection connection = (HttpURLConnection) consumer.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setReadTimeout(Settings.HTTP_DATA_TIMEOUT.getInt());
connection.setDoOutput(true);

String data="";
for (URL node:nodes){
if (data.length()>0)
data+="&";
data+=URLEncoder.encode("url", "UTF-8") + "=" + URLEncoder.encode(node.toString(), "UTF-8");

};

String data = "";
for (URL node : nodes) {
if (data.length() > 0)
data += "&";
data += URLEncoder.encode("url", "UTF-8") + "=" + URLEncoder.encode(node.toString(), "UTF-8");
}
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data);
wr.flush();
wr.close();

return connection;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.vamdc.portal.session.preview;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -45,6 +46,22 @@ private URL getQuery(URL mirror) {

return result;
}

private HttpURLConnection getHttpURLConnection(URL url) throws IOException {
HttpURLConnection connection = null;


//http request
if (!url.getProtocol().equals("https")) {
connection = (HttpURLConnection) url.openConnection();
}
//https request
else {
connection = (HttpsURLConnection) url.openConnection();
}

return connection;
}

/**
* Send a HEAD request to a node by testing its mirrors
Expand All @@ -54,21 +71,19 @@ public HttpHeadResponse call() throws Exception {
HttpHeadResponse response = null;
for (URL mirror : mirrors) {
HttpURLConnection connection = null;
URL queryURL = getQuery(mirror);

//http request
if (!mirror.getProtocol().equals("https")) {
connection = (HttpURLConnection) queryURL.openConnection();
}
//https request
else {
connection = (HttpsURLConnection) queryURL.openConnection();
}

connection = this.getHttpURLConnection(getQuery(mirror));
connection.setRequestMethod("HEAD");
connection.setReadTimeout(Settings.HTTP_HEAD_TIMEOUT.getInt());

/*if(connection.getResponseCode() == 301 || connection.getResponseCode() == 302) {
connection.disconnect();
String newUrl = connection.getHeaderField("Location");
System.out.println("### try new url : " + newUrl);
connection = this.getHttpURLConnection(new URL(newUrl));
connection.setRequestMethod("HEAD");
connection.setReadTimeout(Settings.HTTP_HEAD_TIMEOUT.getInt());
}*/
response = new HttpHeadResponse(ivoaID, connection);

if (response.isOk())
return response;
}
Expand Down
33 changes: 18 additions & 15 deletions portal.ejb/src/main/java/org/vamdc/portal/species/NodeSpecies.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,26 @@ 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);
System.out.println(queryUrl);
resultBean.setFormattedResult( formatRequestResult(queryUrl));
resultBean.setMessage("");
resultBean.setMessage("");
resultBean.setReady(true);
return;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
resultBean.setMessage("Incorrect service URL "+queryUrl);
} catch (IOException e) {
// TODO Auto-generated catch block

// TODO Auto-generated catch block
e.printStackTrace();
resultBean.setMessage("Error while proceeding request");
} catch (TransformerException e) {
Expand All @@ -102,9 +98,14 @@ public void querySpecies(String ivoaId, SpeciesResult resultBean){
if(e.getCode() == 204) {
resultBean.setMessage("Request returned an empty response");
} else if(e.getCode() >= 400){
System.out.println(e);
resultBean.setMessage("Node returned the error " +e.getCode() +" while processing the request "+queryUrl);
}
}finally{
}catch(Exception e) {
System.out.println(e);
resultBean.setMessage("An exception occurred");
}
finally{
resultBean.setReady(true);
}
}
Expand All @@ -117,11 +118,8 @@ public void querySpecies(String ivoaId, SpeciesResult resultBean){
* @throws IOException
* @throws TransformerException
*/
private static String formatRequestResult(URL url) throws IOException, TransformerException, PortalHttpException{
System.out.print("###");
System.out.print(url);
private/* static */ String formatRequestResult(URL url) throws IOException, TransformerException, PortalHttpException{
HttpURLConnection c = (HttpURLConnection)url.openConnection();

if(c.getResponseCode() == 200){
c.setConnectTimeout(Settings.HTTP_CONNECT_TIMEOUT.getInt());
c.setReadTimeout(Settings.HTTP_DATA_TIMEOUT.getInt());
Expand All @@ -137,7 +135,12 @@ private static String formatRequestResult(URL url) throws IOException, Transform

return bOut.toString();
}else{
throw new PortalHttpException("Http error while executing TAP request", c.getResponseCode());
if(c.getResponseCode() == 301 || c.getResponseCode() == 302){
String newUrl = c.getHeaderField("Location");
return this.formatRequestResult(new URL(newUrl));
}else {
throw new PortalHttpException("Http error while executing TAP request", c.getResponseCode());
}
}
}
}

0 comments on commit 01698a2

Please sign in to comment.