Search in sources :

Example 26 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class ItemManager method findVisibleAncestors.

private List<Item> findVisibleAncestors(Item item) {
    List<Item> items = new ArrayList<>();
    List<String> idList = findVisibleAncestorIds(item);
    for (String result : idList) {
        Item ancestor = load(result);
        if (ancestor != null) {
            items.add(ancestor);
        }
    }
    return items;
}
Also used : Item(gov.usgs.cida.coastalhazards.model.Item) ArrayList(java.util.ArrayList)

Example 27 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class HealthResource method healthCheck.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response healthCheck() {
    Response response;
    boolean overallHealth = true;
    Map<String, Object> componentCheckMap = new TreeMap<>();
    try {
        EntityManagerFactory emf = JPAHelper.getEntityManagerFactory();
        boolean open = emf.isOpen();
        componentCheckMap.put("EntityManagerFactory", open);
        overallHealth = open && overallHealth;
    } catch (Exception e) {
        LOG.warn("Exception occurred while checking health", e);
        componentCheckMap.put("EntityManagerFactory", false);
        overallHealth = false;
    }
    try {
        Gson defaultGson = GsonUtil.getDefault();
        boolean ok = (defaultGson != null);
        componentCheckMap.put("DefaultGson", ok);
        overallHealth = ok && overallHealth;
    } catch (Exception e) {
        LOG.warn("Exception occurred while checking health", e);
        componentCheckMap.put("DefaultGson", false);
        overallHealth = false;
    }
    try {
        Gson idGson = GsonUtil.getIdOnlyGson();
        boolean ok = (idGson != null);
        componentCheckMap.put("NonSubtreeGson", ok);
        overallHealth = ok && overallHealth;
    } catch (Exception e) {
        LOG.warn("Exception occurred while checking health", e);
        componentCheckMap.put("NonSubtreeGson", false);
        overallHealth = false;
    }
    try {
        Gson subtreeGson = GsonUtil.getSubtreeGson();
        boolean ok = (subtreeGson != null);
        componentCheckMap.put("SubtreeGson", ok);
        overallHealth = ok && overallHealth;
    } catch (Exception e) {
        LOG.warn("Exception occurred while checking health", e);
        componentCheckMap.put("SubtreeGson", false);
        overallHealth = false;
    }
    try (ItemManager im = new ItemManager()) {
        Item uber = im.load("uber");
        boolean ok = (uber != null && uber.isEnabled());
        componentCheckMap.put("ItemManager", ok);
        overallHealth = ok && overallHealth;
    } catch (Exception e) {
        LOG.warn("Exception occurred while checking health", e);
        componentCheckMap.put("ItemManager", false);
        overallHealth = false;
    }
    try {
        Map<String, Boolean> geoserverStatus = new HashMap<>();
        GeoServerRESTReader rest = new GeoServerRESTReader(geoserverEndpoint, geoserverUser, geoserverPass);
        boolean existGeoserver = rest.existGeoserver();
        boolean workspacesConfigured = rest.getWorkspaceNames().contains("proxied");
        // TODO may want to add some more checks
        geoserverStatus.put("up", existGeoserver);
        geoserverStatus.put("configured", workspacesConfigured);
        componentCheckMap.put("Geoserver", geoserverStatus);
        overallHealth = overallHealth && existGeoserver && workspacesConfigured;
    } catch (Exception e) {
        LOG.warn("Exception occurred while checking health", e);
        componentCheckMap.put("Geoserver", false);
        overallHealth = false;
    }
    try {
        // health check add for pycsw Jira cchs-306
        boolean hasCswGetCapabilities = false;
        Map<String, Boolean> pyCswStatus = new HashMap<>();
        String endpointTest = pycswEndpoint + "?service=CSW&request=GetCapabilities&version=" + pycswVersion;
        HttpGet httpGet = new HttpGet(endpointTest);
        HttpClient httpclient = new DefaultHttpClient();
        // Create a custom response handler
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

            @Override
            public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };
        // close anonymous inner class
        String resp = httpclient.execute(httpGet, responseHandler);
        hasCswGetCapabilities = resp != null;
        pyCswStatus.put("getCapabilities", hasCswGetCapabilities);
        componentCheckMap.put("PyCsw", pyCswStatus);
        overallHealth = overallHealth && hasCswGetCapabilities;
    } catch (Exception e) {
        LOG.warn("Exception occurred while checking csw health", e);
        componentCheckMap.put("Pycsw", false);
        overallHealth = false;
    }
    try (StatusManager statusMan = new StatusManager()) {
        // NOTE this does not effect the overall health
        boolean staleCache = false;
        Map<Status.StatusName, Status> statuses = statusMan.loadAll();
        Status itemLastUpdate = statuses.get(StatusName.ITEM_UPDATE);
        Status structureLastUpdate = statuses.get(StatusName.STRUCTURE_UPDATE);
        Status cacheClearedDate = statuses.get(StatusName.CACHE_CLEAR);
        Date itemOrStructureUpdate = null;
        if (itemLastUpdate != null) {
            itemOrStructureUpdate = itemLastUpdate.getLastUpdate();
        }
        if (structureLastUpdate != null) {
            Date structureDate = structureLastUpdate.getLastUpdate();
            if (structureDate != null && itemOrStructureUpdate != null && structureDate.after(itemOrStructureUpdate)) {
                itemOrStructureUpdate = structureDate;
            }
        }
        if (cacheClearedDate != null) {
            Date cacheDate = cacheClearedDate.getLastUpdate();
            if (cacheDate != null && itemOrStructureUpdate != null && cacheDate.before(itemOrStructureUpdate)) {
                staleCache = true;
            }
        }
        componentCheckMap.put("TileCacheStale", staleCache);
    }
    Gson gson = GsonUtil.getDefault();
    String json = gson.toJson(componentCheckMap);
    if (overallHealth) {
        response = Response.ok(json, MediaType.APPLICATION_JSON_TYPE).build();
    } else {
        response = Response.serverError().entity(json).build();
    }
    return response;
}
Also used : ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) HttpGet(org.apache.http.client.methods.HttpGet) Gson(com.google.gson.Gson) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) Item(gov.usgs.cida.coastalhazards.model.Item) GeoServerRESTReader(it.geosolutions.geoserver.rest.GeoServerRESTReader) Status(gov.usgs.cida.coastalhazards.model.util.Status) StatusManager(gov.usgs.cida.coastalhazards.jpa.StatusManager) ItemManager(gov.usgs.cida.coastalhazards.jpa.ItemManager) HttpResponse(org.apache.http.HttpResponse) StatusName(gov.usgs.cida.coastalhazards.model.util.Status.StatusName) TreeMap(java.util.TreeMap) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) Date(java.util.Date) Response(javax.ws.rs.core.Response) HttpResponse(org.apache.http.HttpResponse) EntityManagerFactory(javax.persistence.EntityManagerFactory) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 28 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class PcoiTest method testGenerateSLD.

@Test
public void testGenerateSLD() {
    Item item = new Item();
    item.setAttr("PCOL1");
    item.setId("abcde");
    item.setType(Item.Type.storms);
    List<Service> services = new LinkedList<>();
    Service wmsService = new Service();
    wmsService.setEndpoint("http://test");
    wmsService.setServiceParameter("0");
    wmsService.setType(ServiceType.source_wms);
    services.add(wmsService);
    item.setServices(services);
    Summary summary = new Summary();
    Tiny tiny = new Tiny();
    tiny.setText("Pcoi");
    summary.setTiny(tiny);
    item.setSummary(summary);
    SLDGenerator pcoi = new SLDGenerator(item, item.getId(), null, Pcoi.pcoi);
    Response response = pcoi.generateSLD();
    // TODO?
    Viewable sld = (Viewable) response.getEntity();
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Viewable(org.glassfish.jersey.server.mvc.Viewable) Service(gov.usgs.cida.coastalhazards.model.Service) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 29 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class PcoiTest method testGenerateSLDInfo.

/**
 * Test of generateSLDInfo method, of class Pcoi.
 */
@Test
public void testGenerateSLDInfo() {
    Item item = new Item();
    item.setAttr("PCOL1");
    item.setId("abcde");
    item.setType(Item.Type.storms);
    List<Service> services = new LinkedList<>();
    Service wmsService = new Service();
    wmsService.setEndpoint("http://test");
    wmsService.setServiceParameter("0");
    wmsService.setType(ServiceType.source_wms);
    services.add(wmsService);
    item.setServices(services);
    Summary summary = new Summary();
    Tiny tiny = new Tiny();
    tiny.setText("Pcoi");
    summary.setTiny(tiny);
    item.setSummary(summary);
    SLDGenerator pcoi = new SLDGenerator(item, item.getId(), null, Pcoi.pcoi);
    Response response = pcoi.generateSLDInfo();
    String json = (String) response.getEntity();
    Map<String, Object> sldInfo = new Gson().fromJson(json, HashMap.class);
    List<Object> bins = (List) sldInfo.get("bins");
    Map<String, Object> bin = (Map) bins.get(0);
    Double lowerBound = (Double) bin.get("lowerBound");
    String color = (String) bin.get("color");
    assertEquals(lowerBound, 0.0f, 0.01f);
    assertEquals(color, "#FFFFFE");
}
Also used : Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Service(gov.usgs.cida.coastalhazards.model.Service) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 30 with Item

use of gov.usgs.cida.coastalhazards.model.Item in project coastal-hazards by USGS-CIDA.

the class RasterTest method testGenerateSLD.

@Test
public void testGenerateSLD() {
    Item item = new Item();
    // for rasters, this will stay the same. We may need a way to manage the type for a raster(ramp, intervals) etc as that is the volatile piece of data.
    item.setAttr("GRAY_INDEX");
    // may want h
    item.setId("EHr6oLT5");
    item.setType(Item.Type.vulnerability);
    List<Service> services = new LinkedList<>();
    Service wmsService = new Service();
    wmsService.setEndpoint("http://test");
    wmsService.setServiceParameter("0");
    wmsService.setType(Service.ServiceType.source_wms);
    services.add(wmsService);
    item.setServices(services);
    Summary summary = new Summary();
    Tiny tiny = new Tiny();
    tiny.setText("Coastal response to Sea Level");
    summary.setTiny(tiny);
    item.setSummary(summary);
    SLDGenerator raster = new SLDGenerator(item, item.getId(), null, RasterAE.rasterConfig);
    Response response = raster.generateSLD();
    Viewable sld = (Viewable) response.getEntity();
}
Also used : Response(javax.ws.rs.core.Response) Item(gov.usgs.cida.coastalhazards.model.Item) Tiny(gov.usgs.cida.coastalhazards.model.summary.Tiny) Viewable(org.glassfish.jersey.server.mvc.Viewable) Service(gov.usgs.cida.coastalhazards.model.Service) Summary(gov.usgs.cida.coastalhazards.model.summary.Summary) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Item (gov.usgs.cida.coastalhazards.model.Item)60 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)26 Response (javax.ws.rs.core.Response)24 Path (javax.ws.rs.Path)22 LinkedList (java.util.LinkedList)21 GET (javax.ws.rs.GET)20 Produces (javax.ws.rs.Produces)20 HashMap (java.util.HashMap)16 BadRequestException (gov.usgs.cida.coastalhazards.exception.BadRequestException)12 Summary (gov.usgs.cida.coastalhazards.model.summary.Summary)12 JsonObject (com.google.gson.JsonObject)10 ArrayList (java.util.ArrayList)10 Viewable (org.glassfish.jersey.server.mvc.Viewable)10 Gson (com.google.gson.Gson)9 Service (gov.usgs.cida.coastalhazards.model.Service)8 NotFoundException (javax.ws.rs.NotFoundException)8 Test (org.junit.Test)8 JsonElement (com.google.gson.JsonElement)7 StatusManager (gov.usgs.cida.coastalhazards.jpa.StatusManager)7 Status (gov.usgs.cida.coastalhazards.model.util.Status)7