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"/>
<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>
</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(
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,
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.IndexLayerDV;
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.ui.util.OLGeoJSONUtil;
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.ClickHandler;
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.uibinder.client.UiBinder;
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.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
@ -64,9 +61,6 @@ public class CrossFilteringLayerPanel extends Composite {
public static String COLORSCALERANGE = "COLORSCALERANGE";
// @UiField
// WellForm panelContainer;
@UiField
Fieldset fieldSet;
@ -218,19 +212,14 @@ public class CrossFilteringLayerPanel extends Composite {
}
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() {
public void onError(Request request, Throwable exception) {
// Code omitted for clarity
}
public void onResponseReceived(Request request, Response response) {
GWT.log("wfsURL response: " + response.getText());
Feature[] features = OLGeoJSONUtil.readGeoJsonFeatures(MAP_PROJECTION.EPSG_4326,
response.getText());
@Override
public void onSuccess(String response) {
GWT.log("wfsURL response: " + response);
Feature[] features = OLGeoJSONUtil.readGeoJsonFeatures(MAP_PROJECTION.EPSG_4326, response);
for (Feature feature : features) {
JsPropertyMap<Object> properties = feature.getProperties();
@ -258,12 +247,20 @@ public class CrossFilteringLayerPanel extends Composite {
mapInnestedFeatures.put(level, mapSelectableFeatures);
String placholder = placeholderLayer(layersIDV.get(0)); // Expected one
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);
IndexLayerDV indexLayer = gCubeCollection.getIndexes().get(0);
applicationBus.fireEvent(new ApplyCQLToLayerMapEvent(indexLayer, setCqlFilter));
try {
GCubeSDILayer layer = indexLayer.getLayer();
@ -437,32 +433,32 @@ public class CrossFilteringLayerPanel extends Composite {
} catch (Exception e) {
// TODO: handle exception
}
applicationBus.fireEvent(new ApplyCQLToLayerMapEvent(indexLayer, setCqlFilter));
}
public void showCountResultsOfWFSCrossFiltering(String wfsQuery) {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, wfsQuery);
panelResults.clear();
panelResults.add(new HTML("<hr>"));
panelResults.add(new LoaderIcon("Filtering..."));
try {
panelResults.add(new LoaderIcon("Applying spatial filter..."));
Request response = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// showLoading(false);
// Window.alert(caught.getMessage());
GeoportalDataViewerServiceAsync.Util.getInstance().getHTTPResponseForURL(wfsQuery, new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
panelResults.clear();
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.setClose(false);
panelResults.add(alert);
}
public void onResponseReceived(Request request, Response response) {
Feature[] features = OLGeoJSONUtil.readGeoJsonFeatures(MAP_PROJECTION.EPSG_4326,
response.getText());
@Override
public void onSuccess(String response) {
Feature[] features = OLGeoJSONUtil.readGeoJsonFeatures(MAP_PROJECTION.EPSG_4326, response);
if (features != null) {
int dataCount = features.length;
@ -470,31 +466,24 @@ public class CrossFilteringLayerPanel extends Composite {
panelResults.clear();
panelResults.add(new HTML("<hr>"));
if (dataCount == 0) {
panelResults.add(new HTML("No result found"));
return;
}
String message = "";
if (dataCount > 0) {
if (dataCount <= 0) {
message = "No item found";
} else {
message = "Found " + dataCount;
message += dataCount > 1 ? " centroids" : " centroid";
message += dataCount > 1 ? " items" : " item";
message += ". ";
}
HTML resultMessage = new HTML(message);
HTML resultMessage = new HTML();
resultMessage.getElement().addClassName("search_result_msg");
resultMessage.setHTML(message);
panelResults.add(resultMessage);
}
}
});
} catch (RequestException e) {
panelResults.clear();
}
}
/**

View File

@ -23,10 +23,10 @@ public class OLGeoJSONUtil {
* Builds the geo JSON.
*
* @param projection the projection
* @param geoJSONString the geo JSON string
* @return the geo json
*/
public static GeoJson buildGeoJSON(MAP_PROJECTION projection) {
try {
GeoJsonFeatureOptions fo = new GeoJsonFeatureOptions();
ProjectionOptions projectionOptions = new ProjectionOptions();
projectionOptions.setCode(projection.getName());
@ -37,6 +37,10 @@ public class OLGeoJSONUtil {
geoJsonOpt.setDefaultDataProjection(fp);
geoJsonOpt.setFeatureProjection(fp);
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;
/**
* The Class URLUtil.
* The Class HTTPRequestUtil.
*
* @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.useCaseDescriptors;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
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.mongoservice.GeoportalServiceIdentityProxy;
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.TemporalComparatorUtil;
import org.gcube.portlets.user.geoportaldataviewer.shared.GCubeCollection;
@ -1825,6 +1822,22 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme
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.
*
@ -1846,39 +1859,7 @@ public class GeoportalDataViewerServiceImpl extends RemoteServiceServlet impleme
}
String url = getWFSRequest(layerItem, mapSrsName, mapBBOX, maxFeatures, outputFormat);
StringBuffer response = new StringBuffer();
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;
return HTTPRequestUtil.getResponse(url);
}
}

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;
/**
* The Class URLUtil.
* The Class URLParserUtil.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*