Search in sources :

Example 1 with WMSHeaderParam

use of org.geosdi.geoplatform.services.request.WMSHeaderParam in project geo-platform by geosdi.

the class OGCService method getCapabilitiesAuth.

@Override
public ArrayList<? extends GPLayerGrid> getCapabilitiesAuth(String serverUrl, HttpServletRequest httpServletRequest, Long idServer) throws GeoPlatformException {
    try {
        HttpSession session = httpServletRequest.getSession();
        String token = (String) session.getAttribute("GOOGLE_TOKEN");
        /**
         *@TODO think a way to have this configured*
         */
        String authValue = httpServletRequest.getHeader("iv-user");
        // Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
        List<WMSHeaderParam> headerParams = Lists.newArrayList();
        if ((authValue != null) && !(authValue.trim().isEmpty())) {
            headerParams.add(new WMSHeaderParam("iv-user", authValue));
        }
        // List<WMSHeaderParam> headerKeyValues = Collections.list(headerNames)
        // .stream()
        // .filter(key -> ((httpServletRequest.getHeader(key) != null)))
        // .map(key -> new WMSHeaderParam(key, httpServletRequest.getHeader(key)))
        // .collect(toList());
        logger.trace("###########################HEADERS_TO_PASS_TO_SERVICE : {}\n", headerParams);
        RequestByID req = new RequestByID(idServer);
        GSAccount gsAccount = this.sessionUtility.getLoggedAccount(httpServletRequest).getGsAccount();
        String authKey = null;
        if (gsAccount != null) {
            authKey = gsAccount.getAuthkey();
        }
        ServerDTO server = geoPlatformWMSServiceClient.getCapabilitiesAuth(serverUrl, req, token, authKey, headerParams);
        return dtoServerConverter.createRasterLayerList(server.getLayerList());
    } catch (ResourceNotFoundFault ex) {
        logger.error("Error GetCapabilities: " + ex);
        throw new GeoPlatformException(ex.getMessage());
    } catch (GPSessionTimeout timeout) {
        throw new GeoPlatformException(timeout);
    }
}
Also used : WMSHeaderParam(org.geosdi.geoplatform.services.request.WMSHeaderParam) ServerDTO(org.geosdi.geoplatform.response.ServerDTO) HttpSession(javax.servlet.http.HttpSession) ResourceNotFoundFault(org.geosdi.geoplatform.exception.ResourceNotFoundFault) GSAccount(org.geosdi.geoplatform.core.model.GSAccount) GPSessionTimeout(org.geosdi.geoplatform.gui.utility.GPSessionTimeout) GeoPlatformException(org.geosdi.geoplatform.gui.global.GeoPlatformException) RequestByID(org.geosdi.geoplatform.request.RequestByID)

Example 2 with WMSHeaderParam

use of org.geosdi.geoplatform.services.request.WMSHeaderParam in project geo-platform by geosdi.

the class GeoSDIHttpClient5 method get.

/**
 * Executes an HTTP GET request against the provided URL and returns the server response.
 *
 * <p>If an HTTP authentication {@link #getUser() user} and {@link #getPassword() password} is
 * set, the appropriate authentication HTTP header will be sent with the request.
 *
 * <p>If a {@link #getConnectTimeout() connection timeout} is set, the http connection will be
 * set to respect that timeout.
 *
 * <p>If a {@link #getReadTimeout() read timeout} is set, the http connection will be set to
 * respect it.
 *
 * @param url the URL to retrieve
 * @return an {@link HTTPResponse} encapsulating the response to the HTTP GET request
 */
@Override
public HTTPResponse get(URL url) throws IOException {
    logger.info("Inject OpenAM Cookie Method [GET]");
    HttpGet httpGet = new HttpGet(url.toExternalForm());
    if (this.tryGzip) {
        httpGet.setHeader("Accept-Encoding", "gzip");
    }
    logger.info("HEADERS : " + this.headers);
    if ((this.headers != null) && !(this.headers.isEmpty())) {
        for (WMSHeaderParam wmsHeaderParam : this.headers) {
            httpGet.setHeader(wmsHeaderParam.getHeaderKey(), wmsHeaderParam.getHeaderValue());
        }
    }
    CloseableHttpResponse response = null;
    if (((this.user != null) && !(this.user.trim().isEmpty())) && ((this.password != null) && !(this.password.trim().isEmpty()))) {
        logger.trace("############################Using BasicAuth with user : {} - password : {}\n", this.user, this.password);
        try {
            URI uri = url.toURI();
            HttpClientContext localContext = create();
            HttpHost targetHost = new HttpHost(uri.getScheme(), uri.getHost(), this.retrieveNoSetPort(uri));
            BasicScheme basicAuth = new BasicScheme();
            basicAuth.initPreemptive(new UsernamePasswordCredentials(this.user, this.password.toCharArray()));
            localContext.resetAuthExchange(targetHost, basicAuth);
            response = this.httpClient.execute(targetHost, httpGet, localContext);
        } catch (URISyntaxException ex) {
            throw new IOException("URISyntaxException error : " + ex.getMessage() + " for URL " + url.toExternalForm());
        }
    } else {
        response = this.httpClient.execute(httpGet);
    }
    int responseCode = response.getCode();
    if (200 != responseCode) {
        response.close();
        throw new IOException("Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm());
    } else {
        return new GeoSDIHttpClient5.HttpMethodResponse(response);
    }
}
Also used : WMSHeaderParam(org.geosdi.geoplatform.services.request.WMSHeaderParam) BasicScheme(org.apache.hc.client5.http.impl.auth.BasicScheme) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Aggregations

WMSHeaderParam (org.geosdi.geoplatform.services.request.WMSHeaderParam)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HttpSession (javax.servlet.http.HttpSession)1 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)1 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)1 BasicScheme (org.apache.hc.client5.http.impl.auth.BasicScheme)1 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)1 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 GSAccount (org.geosdi.geoplatform.core.model.GSAccount)1 ResourceNotFoundFault (org.geosdi.geoplatform.exception.ResourceNotFoundFault)1 GeoPlatformException (org.geosdi.geoplatform.gui.global.GeoPlatformException)1 GPSessionTimeout (org.geosdi.geoplatform.gui.utility.GPSessionTimeout)1 RequestByID (org.geosdi.geoplatform.request.RequestByID)1 ServerDTO (org.geosdi.geoplatform.response.ServerDTO)1