All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.unomi.geonames.rest.GeonamesEndPoint Maven / Gradle / Ivy

Go to download

REST API for the Apache Unomi Context Server extension that integrates with the Geonames database

There is a newer version: 2.6.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.unomi.geonames.rest;

import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing;
import org.apache.unomi.api.PartialList;
import org.apache.unomi.geonames.services.GeonameEntry;
import org.apache.unomi.geonames.services.GeonamesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@WebService
@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
@CrossOriginResourceSharing(
        allowAllOrigins = true,
        allowCredentials = true
)
public class GeonamesEndPoint {

    private static final Logger logger = LoggerFactory.getLogger(GeonamesEndPoint.class.getName());

    private GeonamesService geonamesService;

    public GeonamesEndPoint() {
        logger.info("Initializing geonames service endpoint...");
    }

    @WebMethod(exclude = true)
    public void setGeonamesService(GeonamesService geonamesService) {
        this.geonamesService = geonamesService;
    }

    @GET
    @Path("/reverseGeoCode/{latlon}")
    public List reverseGeoCode(@PathParam("latlon") String latlon, @HeaderParam("Accept-Language") String language) {
        String[] s = latlon.split(",");
        List entries = geonamesService.reverseGeoCode(s[0], s[1]);
        translate(entries, new Locale(language));
        return entries;
    }

    @GET
    @Path("/entries/{items:.*}")
    public PartialList getChildrenEntries(@PathParam("items") List items, @HeaderParam("Accept-Language") String language) {
        List l = new ArrayList<>();
        for (PathSegment item : items) {
            l.add(item.getPath());
        }
        PartialList list = geonamesService.getChildrenEntries(l, 0, 999);
        translate(list.getList(), new Locale(language));
        return list;
    }

    @GET
    @Path("/cities/{items:.*}")
    public PartialList getChildrenCities(@PathParam("items") List items, @HeaderParam("Accept-Language") String language) {
        List l = new ArrayList<>();
        for (PathSegment item : items) {
            l.add(item.getPath());
        }
        PartialList list = geonamesService.getChildrenCities(l, 0, 999);
        translate(list.getList(), new Locale(language));
        return list;
    }

    @GET
    @Path("/hierarchy/{id}")
    public List getHierarchy(@PathParam("id") String id, @HeaderParam("Accept-Language") String language) {
        List list = geonamesService.getHierarchy(id);
        translate(list, new Locale(language));
        return list;
    }

    @GET
    @Path("/capitals/{id}")
    public List getCapitalEntries(@PathParam("id") String id, @HeaderParam("Accept-Language") String language) {
        List list = geonamesService.getCapitalEntries(id);
        translate(list, new Locale(language));
        return list;
    }

    private void translate(List l, Locale locale) {
        for (GeonameEntry entry : l) {
            if (GeonamesService.COUNTRY_FEATURE_CODES.contains(entry.getFeatureCode())) {
                String name = new Locale("", entry.getCountryCode()).getDisplayCountry(locale);
                if (!StringUtils.isEmpty(name) && !name.equals(entry.getCountryCode())) {
                    entry.setName(name);
                }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy