Passing WFS requests from the servlet due to CORS issue, see

#25074?#note-7
This commit is contained in:
Francesco Mangiacrapa 2023-06-05 16:54:40 +02:00
parent 36bfcd2b9e
commit a28d2433a0
9 changed files with 214 additions and 151 deletions

View File

@ -61,6 +61,7 @@
@ -145,6 +146,7 @@
@ -229,6 +231,7 @@
@ -313,6 +316,7 @@
@ -397,6 +401,7 @@
@ -481,6 +486,7 @@
@ -503,7 +509,7 @@
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="geoportal-data-common-2.2.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/geoportal-data-common/geoportal-data-common"> <dependent-module archiveName="geoportal-data-mapper-1.0.0.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/geoportal-data-mapper/geoportal-data-mapper">
<dependency-type>uses</dependency-type> <dependency-type>uses</dependency-type>
</dependent-module> </dependent-module>
@ -568,6 +574,7 @@
@ -652,6 +659,7 @@
@ -736,6 +744,7 @@
@ -820,6 +829,7 @@

View File

@ -219,4 +219,12 @@ public interface GeoportalDataViewerService extends RemoteService {
Map<String, List<GroupedLayersDV<? extends LayerIDV>>> getAvaiableCustomGroupedLayersForUCD( Map<String, List<GroupedLayersDV<? extends LayerIDV>>> getAvaiableCustomGroupedLayersForUCD(
GEOPORTAL_DATA_HANDLER theHandler); GEOPORTAL_DATA_HANDLER theHandler);
/**
* Gets the HTTP response for URL.
*
* @param url the url
* @return the HTTP response for URL
*/
String getHTTPResponseForURL(String url);
} }

View File

@ -89,4 +89,6 @@ public interface GeoportalDataViewerServiceAsync {
void getAvaiableCustomGroupedLayersForUCD(GEOPORTAL_DATA_HANDLER theHandler, void getAvaiableCustomGroupedLayersForUCD(GEOPORTAL_DATA_HANDLER theHandler,
AsyncCallback<Map<String, List<GroupedLayersDV<? extends LayerIDV>>>> callback); AsyncCallback<Map<String, List<GroupedLayersDV<? extends LayerIDV>>>> callback);
void getHTTPResponseForURL(String url, AsyncCallback<String> callback);
} }

View File

@ -11,6 +11,7 @@ import org.gcube.application.geoportalcommon.shared.geoportal.config.layers.Laye
import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDILayer; import org.gcube.application.geoportalcommon.shared.geoportal.materialization.GCubeSDILayer;
import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayerDV; import org.gcube.application.geoportalcommon.shared.geoportal.materialization.IndexLayerDV;
import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants.MAP_PROJECTION; import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerConstants.MAP_PROJECTION;
import org.gcube.portlets.user.geoportaldataviewer.client.GeoportalDataViewerServiceAsync;
import org.gcube.portlets.user.geoportaldataviewer.client.events.ApplyCQLToLayerMapEvent; import org.gcube.portlets.user.geoportaldataviewer.client.events.ApplyCQLToLayerMapEvent;
import org.gcube.portlets.user.geoportaldataviewer.client.ui.util.OLGeoJSONUtil; import org.gcube.portlets.user.geoportaldataviewer.client.ui.util.OLGeoJSONUtil;
import org.gcube.portlets.user.geoportaldataviewer.client.util.LoaderIcon; import org.gcube.portlets.user.geoportaldataviewer.client.util.LoaderIcon;
@ -37,14 +38,10 @@ import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager; import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL; import com.google.gwt.http.client.URL;
import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField; import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel; import com.google.gwt.user.client.ui.HTMLPanel;
@ -64,9 +61,6 @@ public class CrossFilteringLayerPanel extends Composite {
public static String COLORSCALERANGE = "COLORSCALERANGE"; public static String COLORSCALERANGE = "COLORSCALERANGE";
// @UiField
// WellForm panelContainer;
@UiField @UiField
Fieldset fieldSet; Fieldset fieldSet;
@ -218,19 +212,14 @@ public class CrossFilteringLayerPanel extends Composite {
} }
GWT.log("wfsURL request: " + wfsURL); GWT.log("wfsURL request: " + wfsURL);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, wfsURL);
try { GeoportalDataViewerServiceAsync.Util.getInstance().getHTTPResponseForURL(wfsURL,
new AsyncCallback<String>() {
Request response = builder.sendRequest(null, new RequestCallback() { @Override
public void onError(Request request, Throwable exception) { public void onSuccess(String response) {
// Code omitted for clarity GWT.log("wfsURL response: " + response);
} Feature[] features = OLGeoJSONUtil.readGeoJsonFeatures(MAP_PROJECTION.EPSG_4326, response);
public void onResponseReceived(Request request, Response response) {
GWT.log("wfsURL response: " + response.getText());
Feature[] features = OLGeoJSONUtil.readGeoJsonFeatures(MAP_PROJECTION.EPSG_4326,
response.getText());
for (Feature feature : features) { for (Feature feature : features) {
JsPropertyMap<Object> properties = feature.getProperties(); JsPropertyMap<Object> properties = feature.getProperties();
@ -258,12 +247,20 @@ public class CrossFilteringLayerPanel extends Composite {
mapInnestedFeatures.put(level, mapSelectableFeatures); mapInnestedFeatures.put(level, mapSelectableFeatures);
String placholder = placeholderLayer(layersIDV.get(0)); // Expected one String placholder = placeholderLayer(layersIDV.get(0)); // Expected one
fillListBoxLevel(level, mapSelectableFeatures, listBoxes, placholder); fillListBoxLevel(level, mapSelectableFeatures, listBoxes, placholder);
}
@Override
public void onFailure(Throwable caught) {
panelResults.clear();
HTML error = new HTML(
"Sorry, an issue is occurred on loading data for cross-filtering facility. Error is: "
+ caught.getMessage());
panelResults.add(error);
} }
}); });
} catch (RequestException e) {
// Code omitted for clarity
}
} }
} }
@ -419,7 +416,6 @@ public class CrossFilteringLayerPanel extends Composite {
GWT.log("CQL FILTER built: " + setCqlFilter); GWT.log("CQL FILTER built: " + setCqlFilter);
IndexLayerDV indexLayer = gCubeCollection.getIndexes().get(0); IndexLayerDV indexLayer = gCubeCollection.getIndexes().get(0);
applicationBus.fireEvent(new ApplyCQLToLayerMapEvent(indexLayer, setCqlFilter));
try { try {
GCubeSDILayer layer = indexLayer.getLayer(); GCubeSDILayer layer = indexLayer.getLayer();
@ -437,32 +433,32 @@ public class CrossFilteringLayerPanel extends Composite {
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception // TODO: handle exception
} }
applicationBus.fireEvent(new ApplyCQLToLayerMapEvent(indexLayer, setCqlFilter));
} }
public void showCountResultsOfWFSCrossFiltering(String wfsQuery) { public void showCountResultsOfWFSCrossFiltering(String wfsQuery) {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, wfsQuery);
panelResults.clear(); panelResults.clear();
panelResults.add(new HTML("<hr>")); panelResults.add(new HTML("<hr>"));
panelResults.add(new LoaderIcon("Filtering...")); panelResults.add(new LoaderIcon("Applying spatial filter..."));
try {
Request response = builder.sendRequest(null, new RequestCallback() { GeoportalDataViewerServiceAsync.Util.getInstance().getHTTPResponseForURL(wfsQuery, new AsyncCallback<String>() {
public void onError(Request request, Throwable exception) {
// showLoading(false); @Override
// Window.alert(caught.getMessage()); public void onFailure(Throwable caught) {
panelResults.clear(); panelResults.clear();
panelResults.add(new HTML("<hr>")); panelResults.add(new HTML("<hr>"));
Alert alert = new Alert("Error on returning number of centroids"); Alert alert = new Alert("Error on returning number of items");
alert.setType(AlertType.ERROR); alert.setType(AlertType.ERROR);
alert.setClose(false); alert.setClose(false);
panelResults.add(alert); panelResults.add(alert);
} }
public void onResponseReceived(Request request, Response response) { @Override
public void onSuccess(String response) {
Feature[] features = OLGeoJSONUtil.readGeoJsonFeatures(MAP_PROJECTION.EPSG_4326, Feature[] features = OLGeoJSONUtil.readGeoJsonFeatures(MAP_PROJECTION.EPSG_4326, response);
response.getText());
if (features != null) { if (features != null) {
int dataCount = features.length; int dataCount = features.length;
@ -470,31 +466,24 @@ public class CrossFilteringLayerPanel extends Composite {
panelResults.clear(); panelResults.clear();
panelResults.add(new HTML("<hr>")); panelResults.add(new HTML("<hr>"));
if (dataCount == 0) {
panelResults.add(new HTML("No result found"));
return;
}
String message = ""; String message = "";
if (dataCount > 0) { if (dataCount <= 0) {
message = "No item found";
} else {
message = "Found " + dataCount; message = "Found " + dataCount;
message += dataCount > 1 ? " centroids" : " centroid"; message += dataCount > 1 ? " items" : " item";
message += ". "; message += ". ";
} }
HTML resultMessage = new HTML();
HTML resultMessage = new HTML(message);
resultMessage.getElement().addClassName("search_result_msg"); resultMessage.getElement().addClassName("search_result_msg");
resultMessage.setHTML(message);
panelResults.add(resultMessage); panelResults.add(resultMessage);
} }
} }
}); });
} catch (RequestException e) {
panelResults.clear();
}
} }
/** /**

View File

@ -23,10 +23,10 @@ public class OLGeoJSONUtil {
* Builds the geo JSON. * Builds the geo JSON.
* *
* @param projection the projection * @param projection the projection
* @param geoJSONString the geo JSON string
* @return the geo json * @return the geo json
*/ */
public static GeoJson buildGeoJSON(MAP_PROJECTION projection) { public static GeoJson buildGeoJSON(MAP_PROJECTION projection) {
try {
GeoJsonFeatureOptions fo = new GeoJsonFeatureOptions(); GeoJsonFeatureOptions fo = new GeoJsonFeatureOptions();
ProjectionOptions projectionOptions = new ProjectionOptions(); ProjectionOptions projectionOptions = new ProjectionOptions();
projectionOptions.setCode(projection.getName()); projectionOptions.setCode(projection.getName());
@ -37,6 +37,10 @@ public class OLGeoJSONUtil {
geoJsonOpt.setDefaultDataProjection(fp); geoJsonOpt.setDefaultDataProjection(fp);
geoJsonOpt.setFeatureProjection(fp); geoJsonOpt.setFeatureProjection(fp);
return OLFactory.createGeoJSON(geoJsonOpt); return OLFactory.createGeoJSON(geoJsonOpt);
} catch (Exception e) {
// silent
return OLFactory.createGeoJSON();
}
} }

View File

@ -1,7 +1,7 @@
package org.gcube.portlets.user.geoportaldataviewer.client.util; package org.gcube.portlets.user.geoportaldataviewer.client.util;
/** /**
* The Class URLUtil. * The Class HTTPRequestUtil.
* *
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
* *

View File

@ -3,10 +3,6 @@ package org.gcube.portlets.user.geoportaldataviewer.server;
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.projects; import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.projects;
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.useCaseDescriptors; import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.useCaseDescriptors;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -79,6 +75,7 @@ import org.gcube.portlets.user.geoportaldataviewer.server.gis.GisMakers;
import org.gcube.portlets.user.geoportaldataviewer.server.gis.WMSUrlValidator; import org.gcube.portlets.user.geoportaldataviewer.server.gis.WMSUrlValidator;
import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.GeoportalServiceIdentityProxy; import org.gcube.portlets.user.geoportaldataviewer.server.mongoservice.GeoportalServiceIdentityProxy;
import org.gcube.portlets.user.geoportaldataviewer.server.util.ContextRequest; import org.gcube.portlets.user.geoportaldataviewer.server.util.ContextRequest;
import org.gcube.portlets.user.geoportaldataviewer.server.util.HTTPRequestUtil;
import org.gcube.portlets.user.geoportaldataviewer.server.util.SessionUtil; import org.gcube.portlets.user.geoportaldataviewer.server.util.SessionUtil;
import org.gcube.portlets.user.geoportaldataviewer.server.util.TemporalComparatorUtil; import org.gcube.portlets.user.geoportaldataviewer.server.util.TemporalComparatorUtil;
import org.gcube.portlets.user.geoportaldataviewer.shared.GCubeCollection; import org.gcube.portlets.user.geoportaldataviewer.shared.GCubeCollection;
@ -1825,6 +1822,22 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme
return url; return url;
} }
/**
* Gets the HTTP response for URL.
*
* @param url the url
* @return the HTTP response for URL
*/
@Override
public String getHTTPResponseForURL(String url) {
LOG.info("getHTTPResponseForURL called");
if (LOG.isDebugEnabled()) {
LOG.debug("getHTTPResponseForURL for URL {} ", url);
}
return HTTPRequestUtil.getResponse(url);
}
/** /**
* Gets the WFS response. * Gets the WFS response.
* *
@ -1846,39 +1859,7 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme
} }
String url = getWFSRequest(layerItem, mapSrsName, mapBBOX, maxFeatures, outputFormat); String url = getWFSRequest(layerItem, mapSrsName, mapBBOX, maxFeatures, outputFormat);
StringBuffer response = new StringBuffer(); return HTTPRequestUtil.getResponse(url);
String theResponseString = "";
HttpURLConnection con = null;
LOG.debug("Built URL: " + url);
try {
URL obj = new URL(url);
con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
LOG.debug("GET Response Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} else {
LOG.info("GET request did not work.");
}
theResponseString = response.toString();
// LOG.trace(theResponseString);
if (LOG.isDebugEnabled()) {
LOG.debug("getWFSResponse is empty? " + theResponseString.isEmpty());
}
} catch (Exception e) {
LOG.error("Error on performing the request to URL: " + url, e);
} finally {
}
return theResponseString;
} }
} }

View File

@ -0,0 +1,69 @@
package org.gcube.portlets.user.geoportaldataviewer.server.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.gcube.portlets.user.geoportaldataviewer.server.GeoportalDataViewerServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class HTTPRequestUtil.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jun 5, 2023
*/
public class HTTPRequestUtil {
private static final Logger LOG = LoggerFactory.getLogger(GeoportalDataViewerServiceImpl.class);
/**
* Gets the response.
*
* @param url the url
* @return the response
*/
public static String getResponse(String url) {
if (url == null || url.isEmpty())
return null;
StringBuffer response = new StringBuffer();
String theResponseString = "";
HttpURLConnection con = null;
LOG.debug("Calling URL: " + url);
try {
URL obj = new URL(url);
con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
LOG.debug("GET Response Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} else {
LOG.info("GET request did not work.");
}
theResponseString = response.toString();
// LOG.trace(theResponseString);
if (LOG.isDebugEnabled()) {
LOG.debug("getResponse is empty? " + theResponseString.isEmpty());
}
} catch (Exception e) {
LOG.error("Error on performing the request to URL: " + url, e);
} finally {
}
LOG.debug("returning response with size: " + theResponseString.length());
return theResponseString;
}
}

View File

@ -9,7 +9,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* The Class URLUtil. * The Class URLParserUtil.
* *
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* *