/*
* Constellation - An open source and standard compliant SDI
* http://www.constellation-sdi.org
*
* Copyright 2014 Geomatys.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.constellation.rest.api;
import org.apache.sis.storage.DataStoreException;
import org.apache.sis.util.logging.Logging;
import org.constellation.admin.dto.CoordinateReferenceSystemDTO;
import org.constellation.configuration.StringList;
import org.constellation.dto.CRSCoverageList;
import org.constellation.utils.CRSUtilities;
import org.constellation.ws.CstlServiceException;
import org.constellation.ws.rs.LayerProviders;
import org.geotoolkit.referencing.CRS;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.util.FactoryException;
import org.springframework.stereotype.Component;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* REST API to access to EPSG
*
* @author bgarcia
* @version 0.9
* @since 0.9
*
*/
@Component
@Path("/1/crs")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class CRSRest {
private static final Logger LOGGER = Logging.getLogger("org.constellation.rest.api");
/**
* @return All EPSG CRS
*/
@GET
@Path("all/{start}/{nbByPage}/{filter}")
public Response getAll(@PathParam("start") int start, @PathParam("nbByPage") int nbByPage, @PathParam("filter") String filter){
final CRSCoverageList coverageList = CRSUtilities.pagingAndFilterCode(start, nbByPage, filter);
return Response.ok(coverageList).build();
}
/**
* Return all EPSG code with there description/name.
* This method is similar to {@link org.constellation.rest.api.ProviderRest#getAllEpsgCode(int, String)} but doesn't
* need domain id and provider id which are useless.
*
* @return a list of {@link org.constellation.admin.dto.CoordinateReferenceSystemDTO}
*/
@GET
@Produces("application/json")
public Response getAllEPSG() {
try {
final CRSAuthorityFactory factory = CRS.getAuthorityFactory(Boolean.FALSE);
final Set<String> authorityCodes = factory.getAuthorityCodes(CoordinateReferenceSystem.class);
final List<CoordinateReferenceSystemDTO> crss = new ArrayList<>();
for (String code : authorityCodes) {
final String codeAndName = code + " - " + factory.getDescriptionText(code).toString();
crss.add(new CoordinateReferenceSystemDTO(code, codeAndName));
}
return Response.ok().entity(crss).build();
} catch (FactoryException ex) {
LOGGER.log(Level.WARNING, "Unable to load EPSG codes : "+ex.getMessage(), ex);
return Response.serverError().entity("Unable to load EPSG codes : "+ex.getMessage()).build();
}
}
/**
* Return all EPSG code with there description/name.
* This method is similar to {@link org.constellation.rest.api.ProviderRest#getAllEpsgCode(int, String)} but doesn't
* need domain id and provider id which are useless.
*
* @param filter an optional filter parameter applied on code and crs name.
* @return a list of {@link org.constellation.admin.dto.CoordinateReferenceSystemDTO}
*/
@GET
@Path("/filter/{filter}")
@Produces("application/json")
public Response getAllEPSG(@PathParam("filter") String filter) {
try {
final CRSAuthorityFactory factory = CRS.getAuthorityFactory(Boolean.FALSE);
final Set<String> authorityCodes = factory.getAuthorityCodes(CoordinateReferenceSystem.class);
if (filter != null) {
filter = filter.toLowerCase();
}
final List<CoordinateReferenceSystemDTO> crss = new ArrayList<>();
for (String code : authorityCodes) {
final String codeAndName = code + " - " + factory.getDescriptionText(code).toString();
if (filter != null && codeAndName.toLowerCase().contains(filter)) {
crss.add(new CoordinateReferenceSystemDTO(code, codeAndName));
}
}
return Response.ok().entity(crss).build();
} catch (FactoryException ex) {
LOGGER.log(Level.WARNING, "Enable to load EPSG codes : "+ex.getMessage(), ex);
return Response.serverError().entity("Enable to load EPSG codes : "+ex.getMessage()).build();
}
}
/**
* return crs list string for a layer
* @param providerId provider identifier which contain layer
* @param LayerId layer identifier on provider
* @return crs {@link String} {@link java.util.List}
*/
@GET
@Path("{id}/{layer}")
public Response getCrsList(@PathParam("id") final String providerId, @PathParam("layer") final String LayerId){
List<String> crs;
try {
crs = LayerProviders.getCrs(providerId, LayerId);
} catch (CstlServiceException | IOException | DataStoreException e) {
LOGGER.log(Level.WARNING, "error when search CRS", e);
return Response.status(Response.Status.BAD_REQUEST).build();
}
final StringList sl = new StringList(crs);
return Response.ok(sl).build();
}
}