Search in sources :

Example 1 with GeoServerRESTReader

use of it.geosolutions.geoserver.rest.GeoServerRESTReader 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 2 with GeoServerRESTReader

use of it.geosolutions.geoserver.rest.GeoServerRESTReader in project sldeditor by robward-scisys.

the class GeoServerClient method uploadNewStyle.

/**
 * Upload new style.
 *
 * @param sldBody the sld body
 * @param workspaceName the workspace name
 * @param styleName the style name
 * @param manager the manager
 * @param publisher the publisher
 * @return true, if successful
 */
private boolean uploadNewStyle(String sldBody, String workspaceName, String styleName, GeoServerRESTManager manager, GeoServerRESTPublisher publisher) {
    boolean result;
    if (isDefaultWorkspace(workspaceName)) {
        result = publisher.publishStyle(sldBody, styleName, true);
    } else {
        GeoServerRESTReader reader = manager.getReader();
        if (reader != null) {
            if (!reader.existsWorkspace(workspaceName) && !publisher.createWorkspace(workspaceName)) {
                ConsoleManager.getInstance().error(this, Localisation.getField(GeoServerClient.class, "GeoServerClient.failedToCreateWorkspace") + workspaceName);
                return false;
            }
        } else {
            return false;
        }
        result = publisher.publishStyleInWorkspace(workspaceName, sldBody, styleName);
    }
    return result;
}
Also used : GeoServerRESTReader(it.geosolutions.geoserver.rest.GeoServerRESTReader)

Example 3 with GeoServerRESTReader

use of it.geosolutions.geoserver.rest.GeoServerRESTReader in project sldeditor by robward-scisys.

the class GeoServerClient method getWorkspaceList.

/**
 * Gets the workspace list.
 *
 * @return the workspace list
 */
@Override
public List<String> getWorkspaceList() {
    List<String> workspaceList = null;
    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    if (manager != null) {
        GeoServerRESTReader reader = manager.getReader();
        if (reader != null) {
            workspaceList = reader.getWorkspaceNames();
        }
    }
    if (workspaceList == null) {
        workspaceList = new ArrayList<>();
    }
    return workspaceList;
}
Also used : GeoServerRESTReader(it.geosolutions.geoserver.rest.GeoServerRESTReader) GeoServerRESTManager(it.geosolutions.geoserver.rest.GeoServerRESTManager)

Example 4 with GeoServerRESTReader

use of it.geosolutions.geoserver.rest.GeoServerRESTReader in project sldeditor by robward-scisys.

the class GeoServerClient method styleExists.

/**
 * Check to see if style exists.
 *
 * @param workspaceName the workspace name
 * @param styleName the style name
 * @return true, if successful
 */
private boolean styleExists(String workspaceName, String styleName) {
    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    GeoServerRESTReader reader = manager.getReader();
    if (reader != null) {
        if (isDefaultWorkspace(workspaceName)) {
            return reader.existsStyle(styleName, true);
        } else {
            // Check workspace exists first
            if (reader.existsWorkspace(workspaceName)) {
                return reader.existsStyle(workspaceName, styleName);
            }
        }
    }
    return false;
}
Also used : GeoServerRESTReader(it.geosolutions.geoserver.rest.GeoServerRESTReader) GeoServerRESTManager(it.geosolutions.geoserver.rest.GeoServerRESTManager)

Example 5 with GeoServerRESTReader

use of it.geosolutions.geoserver.rest.GeoServerRESTReader in project sldeditor by robward-scisys.

the class GeoServerClient method retrieveData.

/**
 * Retrieve data from GeoServer.
 */
@Override
public void retrieveData() {
    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    if (manager != null) {
        GeoServerRESTReader reader = manager.getReader();
        if (reader != null) {
            if (parentObj != null) {
                parentObj.startPopulating(connection);
            }
            List<String> localWorkspaceList = getWorkspaceList();
            parseStyleList(reader, localWorkspaceList);
            parseLayerList(reader, localWorkspaceList, null);
        }
    }
}
Also used : GeoServerRESTReader(it.geosolutions.geoserver.rest.GeoServerRESTReader) GeoServerRESTManager(it.geosolutions.geoserver.rest.GeoServerRESTManager)

Aggregations

GeoServerRESTReader (it.geosolutions.geoserver.rest.GeoServerRESTReader)6 GeoServerRESTManager (it.geosolutions.geoserver.rest.GeoServerRESTManager)4 Gson (com.google.gson.Gson)1 StyleWrapper (com.sldeditor.common.data.StyleWrapper)1 ItemManager (gov.usgs.cida.coastalhazards.jpa.ItemManager)1 StatusManager (gov.usgs.cida.coastalhazards.jpa.StatusManager)1 Item (gov.usgs.cida.coastalhazards.model.Item)1 Status (gov.usgs.cida.coastalhazards.model.util.Status)1 StatusName (gov.usgs.cida.coastalhazards.model.util.Status.StatusName)1 RESTLayerList (it.geosolutions.geoserver.rest.decoder.RESTLayerList)1 RESTStyleList (it.geosolutions.geoserver.rest.decoder.RESTStyleList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 GET (javax.ws.rs.GET)1