Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support enabling API version response header #92

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ The REST-API is enabled by default. To protect this interface from unauthorized
A detailed documentation of the REST-API interface and how access is configured is described in section "deegree REST interface"
of the https://download.deegree.org/documentation/3.4.17/html/#anchor-configuration-restapi[deegree webservices handbook].

=== Enable response header with API version

Set the system property ```deegree.oaf.openapi.version_response_header``` to _true_ to enable returning a response header ```API-Version``` with the version of the implemented OGC API Features.

=== Allow access to OpenAPI document from all origins

In case you want to avoid any issues when using the OpenAPI document from other locations due to CORS, you can enable allowing all origins specifically for accessing the OpenAPI document.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*-
* #%L
* deegree-ogcapi-features - OGC API Features (OAF) implementation - Querying and modifying of geospatial data objects
* %%
* Copyright (C) 2019 - 2020 lat/lon GmbH, info@lat-lon.de, www.lat-lon.de
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.deegree.services.oaf.filter;


import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;

import org.deegree.commons.utils.TunableParameter;
import org.deegree.services.oaf.openapi.OpenApiCreator;


/**
* Class to add API version to response headers.
*
* @author Kapil Agnihotri
*/
@Provider
public class ApiVersionResponseFilter implements ContainerResponseFilter {

/**
* Name for parameter that allows enabling returning the API version as response header.
*/
public static final String PARAMETER_ENABLE_VERSION_HEADER = "deegree.oaf.openapi.version_response_header";

private boolean addApiVersionToHeader = TunableParameter.get(PARAMETER_ENABLE_VERSION_HEADER, false);

/**
* Name for response header with API version.
*/
public static final String HEADER_API_VERSION = "API-Version";

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {

if (addApiVersionToHeader) {
responseContext.getHeaders().add(HEADER_API_VERSION, OpenApiCreator.VERSION);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public class OpenApiCreator {

private static final Logger LOG = getLogger( OpenApiCreator.class );

private static final String VERSION = "1.0";
// Implemented version of OGC API Features specification
public static final String VERSION = "1.0.1";

@Context
private ServletConfig servletConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*-
* #%L
* deegree-ogcapi-features - OGC API Features (OAF) implementation - Querying and modifying of geospatial data objects
* %%
* Copyright (C) 2019 - 2020 lat/lon GmbH, info@lat-lon.de, www.lat-lon.de
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.deegree.services.oaf.filter;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE;
import static org.deegree.services.oaf.TestData.mockDataAccess;
import static org.deegree.services.oaf.TestData.mockWorkspaceInitializer;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

import org.deegree.commons.utils.TunableParameter;
import org.deegree.services.oaf.openapi.OpenApiCreator;
import org.deegree.services.oaf.resource.FeatureCollection;
import org.deegree.services.oaf.workspace.DataAccess;
import org.deegree.services.oaf.workspace.DeegreeWorkspaceInitializer;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;

/**
* Base class for testing if API version response header is present.
*/
public abstract class AbstractApiVersionResponseFilterTest extends JerseyTest {

private final boolean headerExpected;

public AbstractApiVersionResponseFilterTest(boolean headerExpected) {
super();
this.headerExpected = headerExpected;
}

@Override
protected Application configure() {
TunableParameter.resetCache();

enable( TestProperties.LOG_TRAFFIC );
ResourceConfig resourceConfig = new ResourceConfig( FeatureCollection.class );
resourceConfig.register(ApiVersionResponseFilter.class);
resourceConfig.register( new AbstractBinder() {
@Override
protected void configure() {
bind( mockDataAccess() ).to( DataAccess.class );
bind( mockWorkspaceInitializer() ).to( DeegreeWorkspaceInitializer.class );
}
} );
return resourceConfig;
}

private void verifyResponse(Response response) {
if (headerExpected) {
assertThat( response.getHeaderString(ApiVersionResponseFilter.HEADER_API_VERSION), is( OpenApiCreator.VERSION ) );
}
else {
assertThat( response.getHeaderString(ApiVersionResponseFilter.HEADER_API_VERSION), is( nullValue() ) );
}
}

@Test
public void test_Collection() {
Response response = target( "/datasets/oaf/collections/test" ).request( APPLICATION_JSON_TYPE ).get();
verifyResponse(response);
}

@Test
public void test_CollectionXml() {
Response response = target( "/datasets/oaf/collections" ).request( APPLICATION_XML_TYPE ).get();
verifyResponse(response);
}

@Test
public void test_Collections() {
Response response = target( "/datasets/oaf/collections" ).request( APPLICATION_JSON_TYPE ).get();
verifyResponse(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*-
* #%L
* deegree-ogcapi-features - OGC API Features (OAF) implementation - Querying and modifying of geospatial data objects
* %%
* Copyright (C) 2019 - 2020 lat/lon GmbH, info@lat-lon.de, www.lat-lon.de
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.deegree.services.oaf.filter;

/**
* Test if API version response header is not present when disabled (default).
*/
public class ApiVersionResponseFilterDisabledTest extends AbstractApiVersionResponseFilterTest {

public ApiVersionResponseFilterDisabledTest() {
super(false);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-
* #%L
* deegree-ogcapi-features - OGC API Features (OAF) implementation - Querying and modifying of geospatial data objects
* %%
* Copyright (C) 2019 - 2020 lat/lon GmbH, info@lat-lon.de, www.lat-lon.de
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.deegree.services.oaf.filter;

import org.junit.AfterClass;
import org.junit.BeforeClass;

/**
* Test if API version response header is present when enabled.
*/
public class ApiVersionResponseFilterEnabledTest extends AbstractApiVersionResponseFilterTest {

@BeforeClass
public static void setProperties() {
System.setProperty( ApiVersionResponseFilter.PARAMETER_ENABLE_VERSION_HEADER, "true" );
}

@AfterClass
public static void resetProperties() {
System.setProperty( ApiVersionResponseFilter.PARAMETER_ENABLE_VERSION_HEADER, "" );
}

public ApiVersionResponseFilterEnabledTest() {
super(true);
}

}