Ignore:
Timestamp:
01/08/16 09:12:35 (9 years ago)
Author:
Twan Goosen
Message:

static getApplicationBaseURI method, now called from JSP configuration template. Fixes #844

Location:
ComponentRegistry/branches/ComponentRegistry-2.0/ComponentRegistry/src/main
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/branches/ComponentRegistry-2.0/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r6887 r6890  
    16151615
    16161616    /**
    1617      *
    1618      * @return The application's base URI as defined by the following context
    1619      * parameters:
    1620      *
    1621      * {@link APPLICATION_URL_BASE_PARAM},
    1622      *     {@link APPLICATION_URL_PATH_PARAM},
    1623      *     {@link APPLICATION_URL_PROTOCOL_HEADER_PARAM}, and
    1624      * {@link APPLICATION_URL_HOST_HEADER_PARAM}
    1625      *
    1626      * If correctly configured, it should look something like
    1627      * "http://catalog.clarin.eu/ds/ComponentRegistry". <em>Be aware that this
    1628      * can also be null if configured incorrectly!</em>
     1617     *
     1618     * @see ComponentRegistryRestService#getApplicationBaseURI(javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest)
    16291619     */
    1630     private synchronized String getApplicationBaseURI() {
    1631         //TODO: Move logic to separate class so that it can also be used in the conig.jsp
    1632         final String path = servletContext.getInitParameter(APPLICATION_URL_PATH_PARAM);
    1633         if (path != null) {
    1634             final String protocolHeader = servletContext.getInitParameter(APPLICATION_URL_PROTOCOL_HEADER_PARAM);
    1635             final String hostHeader = servletContext.getInitParameter(APPLICATION_URL_HOST_HEADER_PARAM);
    1636             if (protocolHeader != null && hostHeader != null) {
    1637 
    1638                 return String.format("%s://%s%s",
    1639                         request.getHeader(protocolHeader),
    1640                         request.getHeader(hostHeader),
    1641                         request.getHeader(path)
    1642                 );
    1643             } else {
    1644                 final String base = servletContext.getInitParameter(APPLICATION_URL_BASE_PARAM);
    1645                 return base + path;
    1646             }
    1647         }
    1648         LOG.error("Application URI could not be determined. Information available:\n"
    1649                 + " {}: {}\n {}: {}\n {}: {}\n {}: {}",
    1650                 APPLICATION_URL_PATH_PARAM, servletContext.getInitParameter(APPLICATION_URL_PATH_PARAM),
    1651                 APPLICATION_URL_BASE_PARAM, servletContext.getInitParameter(APPLICATION_URL_BASE_PARAM),
    1652                 APPLICATION_URL_PROTOCOL_HEADER_PARAM, servletContext.getInitParameter(APPLICATION_URL_PROTOCOL_HEADER_PARAM),
    1653                 APPLICATION_URL_HOST_HEADER_PARAM, servletContext.getInitParameter(APPLICATION_URL_HOST_HEADER_PARAM)
    1654         );
    1655         throw new RuntimeException("Cannot determine application path");
     1620    private String getApplicationBaseURI() {
     1621        return getApplicationBaseURI(servletContext, request);
    16561622    }
    16571623
     
    20251991    }
    20261992
     1993    /**
     1994     *
     1995     * @return The application's base URI as defined by the following context
     1996     * parameters:
     1997     *
     1998     * {@link APPLICATION_URL_BASE_PARAM},
     1999     *     {@link APPLICATION_URL_PATH_PARAM},
     2000     *     {@link APPLICATION_URL_PROTOCOL_HEADER_PARAM}, and
     2001     * {@link APPLICATION_URL_HOST_HEADER_PARAM}
     2002     *
     2003     * If correctly configured, it should look something like
     2004     * "http://catalog.clarin.eu/ds/ComponentRegistry". <em>Be aware that this
     2005     * can also be null if configured incorrectly!</em>
     2006     */
     2007    public static String getApplicationBaseURI(ServletContext servletContext, HttpServletRequest request) {
     2008        final String path = servletContext.getInitParameter(APPLICATION_URL_PATH_PARAM);
     2009        if (path != null) {
     2010            final String protocolHeader = servletContext.getInitParameter(APPLICATION_URL_PROTOCOL_HEADER_PARAM);
     2011            final String hostHeader = servletContext.getInitParameter(APPLICATION_URL_HOST_HEADER_PARAM);
     2012            if (protocolHeader != null && hostHeader != null) {
     2013
     2014                return String.format("%s://%s%s",
     2015                        request.getHeader(protocolHeader),
     2016                        request.getHeader(hostHeader),
     2017                        request.getHeader(path)
     2018                );
     2019            } else {
     2020                final String base = servletContext.getInitParameter(APPLICATION_URL_BASE_PARAM);
     2021                return base + path;
     2022            }
     2023        }
     2024        LOG.error("Application URI could not be determined. Information available:\n"
     2025                + " {}: {}\n {}: {}\n {}: {}\n {}: {}",
     2026                APPLICATION_URL_PATH_PARAM, servletContext.getInitParameter(APPLICATION_URL_PATH_PARAM),
     2027                APPLICATION_URL_BASE_PARAM, servletContext.getInitParameter(APPLICATION_URL_BASE_PARAM),
     2028                APPLICATION_URL_PROTOCOL_HEADER_PARAM, servletContext.getInitParameter(APPLICATION_URL_PROTOCOL_HEADER_PARAM),
     2029                APPLICATION_URL_HOST_HEADER_PARAM, servletContext.getInitParameter(APPLICATION_URL_HOST_HEADER_PARAM)
     2030        );
     2031        throw new RuntimeException("Cannot determine application path");
     2032    }
     2033
    20272034}
  • ComponentRegistry/branches/ComponentRegistry-2.0/ComponentRegistry/src/main/webapp/compRegConfig.jsp

    r6885 r6890  
    11<%-- Configuration hosted next to the front end index page, from which it is loaded --%>
    22<%@ page import="java.net.URI" %>
     3<%@ page import="clarin.cmdi.componentregistry.rest.ComponentRegistryRestService" %>
    34<%
    45    final String loglevelParam = request.getParameter("loglevel");
     
    910        loglevel = "info" ;
    1011    }
     12   
     13    final String appUrl = ComponentRegistryRestService.getApplicationBaseURI(getServletContext(), request);
    1114%>
    1215{
     
    1417  "cors": false,
    1518  "REST": {
    16     "url": "<%= application.getInitParameter("eu.clarin.cmdi.componentregistry.serviceRootUrl") %>"
     19    "url": "<%= appUrl %>"
    1720  },
    1821  "backEndVersion": "${project.version}",
     
    2023  "deploy": {
    2124    "path": "<%=
    22                 URI.create(application.getInitParameter("eu.clarin.cmdi.componentregistry.serviceRootUrl"))
    23                         .getPath() //at same URL as app, so simply take path
     25                URI.create(appUrl).getPath() //at same URL as app, so simply take path
    2426            %>"
    2527  }
Note: See TracChangeset for help on using the changeset viewer.