it.geosolutions.geoserver.rest.GeoServerRESTReader Java Examples

The following examples show how to use it.geosolutions.geoserver.rest.GeoServerRESTReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/** 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);
        }
    }
}
 
Example #2
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets the style.
 *
 * @param styleWrapper the style wrapper
 * @return the style
 */
@Override
public String getStyle(StyleWrapper styleWrapper) {
    if (styleWrapper == null) {
        return null;
    }

    if (!workspaceValid(styleWrapper.getWorkspace())) {
        return null;
    }

    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    if (manager != null) {
        GeoServerRESTReader reader = manager.getReader();
        if (reader != null) {
            if (isDefaultWorkspace(styleWrapper.getWorkspace())) {
                return reader.getSLD(styleWrapper.getStyle());
            } else {
                return reader.getSLD(styleWrapper.getWorkspace(), styleWrapper.getStyle());
            }
        }
    }

    return null;
}
 
Example #3
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses the style in default workspace.
 *
 * @param reader the reader
 * @param count the count
 * @param styleList the style list
 * @return the int
 */
private int parseStyleInDefaultWorkspace(
        GeoServerRESTReader reader, int count, List<StyleWrapper> styleList) {
    // Read styles not in a workspace
    RESTStyleList geoServerStyleList = reader.getStyles();

    for (String style : geoServerStyleList.getNames()) {
        StyleWrapper newStyleWrapper = new StyleWrapper(DEFAULT_WORKSPACE_NAME, style);
        styleList.add(newStyleWrapper);

        if (parentObj != null) {
            parentObj.readStylesProgress(connection, count, count);
        }
        count++;
    }
    return count;
}
 
Example #6
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses the style in workspace.
 *
 * @param reader the reader
 * @param styleMap the style map
 * @param count the count
 * @param workspaceName the workspace name
 * @return the int
 */
private int parseStyleInWorkspace(
        GeoServerRESTReader reader,
        Map<String, List<StyleWrapper>> styleMap,
        int count,
        String workspaceName) {
    List<StyleWrapper> styleList;
    if (workspaceName != null) {
        RESTStyleList geoServerWorkspaceStyleList = reader.getStyles(workspaceName);

        styleList = new ArrayList<>();

        for (String style : geoServerWorkspaceStyleList.getNames()) {
            StyleWrapper newStyleWrapper = new StyleWrapper(workspaceName, style);
            styleList.add(newStyleWrapper);

            if (parentObj != null) {
                parentObj.readStylesProgress(connection, count, count);
            }
            count++;
        }

        styleMap.put(workspaceName, styleList);
    }
    return count;
}
 
Example #7
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses the layer list worker.
 *
 * @param reader the reader
 * @param existingWorkspaceList the existing workspace list
 * @param workspaceName the workspace name
 */
private void parseLayerListWorker(
        GeoServerRESTReader reader, List<String> existingWorkspaceList, String workspaceName) {
    List<String> localWorkspaceList = null;

    if (workspaceName == null) {
        localWorkspaceList = existingWorkspaceList;
        localWorkspaceList.add(null); // Add the default workspace last
    }

    Map<String, List<GeoServerLayer>> layerMap = new LinkedHashMap<>();
    RESTLayerList layers = reader.getLayers();

    parseLayer(reader, localWorkspaceList, layerMap, layers);

    if (parentObj != null) {
        parentObj.readLayersComplete(connection, layerMap);
    }
}
 
Example #8
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets the layer workspace.
 *
 * @param reader the reader
 * @param localWorkspaceList the local workspace list
 * @param layerName the layer name
 * @return the layer workspace
 */
private String getLayerWorkspace(
        GeoServerRESTReader reader, List<String> localWorkspaceList, String layerName) {
    String workspace = null;
    for (String workspaceNameToTest : localWorkspaceList) {
        if (reader.existsLayer(workspaceNameToTest, layerName, true)) {
            if (workspaceNameToTest == null) {
                workspace = DEFAULT_WORKSPACE_NAME;
            } else {
                workspace = workspaceNameToTest;
            }

            break;
        }
    }
    return workspace;
}
 
Example #9
Source File: WorkspacesManagerServiceImpl.java    From geofence with GNU General Public License v2.0 6 votes vote down vote up
public SortedSet<String> getWorkspaceLayers(GeoServerRESTReader reader, String workspace) {

        SortedSet<String> layerNames = new TreeSet<String>();

        RESTFeatureTypeList featureTypes = reader.getFeatureTypes(workspace);
        for (NameLinkElem ft : featureTypes) {
            layerNames.add(ft.getName());
        }

        RESTCoverageStoreList coverageStores = reader.getCoverageStores(workspace);
        for (NameLinkElem csName : coverageStores) {
            RESTCoverageList coverages = reader.getCoverages(workspace, csName.getName());
            for (NameLinkElem coverage : coverages) {
                layerNames.add(coverage.getName());
            }
        }

        return layerNames;
    }
 
Example #10
Source File: LayerGroupCommands.java    From geoserver-shell with MIT License 5 votes vote down vote up
@CliCommand(value = "layer group list", help = "List layer groups.")
public String list(
        @CliOption(key = "workspace", mandatory = false, help = "The workspace") String workspace
) throws Exception {
    StringBuilder builder = new StringBuilder();
    GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
    RESTLayerGroupList layerGroups = workspace == null ? reader.getLayerGroups() : reader.getLayerGroups(workspace);
    List<String> names = layerGroups.getNames();
    Collections.sort(names);
    for (String name : names) {
        builder.append(name + OsUtils.LINE_SEPARATOR);
    }
    return builder.toString();
}
 
Example #11
Source File: LayerCommands.java    From geoserver-shell with MIT License 5 votes vote down vote up
@CliCommand(value = "layer list", help = "List layers.")
public String list() throws Exception {
    GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
    RESTLayerList layers = reader.getLayers();
    List<String> names = layers.getNames();
    Collections.sort(names);
    StringBuilder builder = new StringBuilder();
    for (String name : names) {
        builder.append(name + OsUtils.LINE_SEPARATOR);
    }
    return builder.toString();
}
 
Example #12
Source File: NamespaceCommands.java    From geoserver-shell with MIT License 5 votes vote down vote up
@CliCommand(value = "namespace get", help = "Get a namespace.")
public String get(
        @CliOption(key = "prefix", mandatory = true, help = "The prefix") String prefix) throws Exception {
    GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
    RESTNamespace nm = reader.getNamespace(prefix);
    StringBuilder builder = new StringBuilder();
    builder.append(nm.getPrefix()).append(OsUtils.LINE_SEPARATOR);
    builder.append(nm.getURI());
    return builder.toString();
}
 
Example #13
Source File: NamespaceCommands.java    From geoserver-shell with MIT License 5 votes vote down vote up
@CliCommand(value = "namespace list", help = "List namespaces.")
public String list() throws Exception {
    GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
    List<String> names = reader.getNamespaceNames();
    Collections.sort(names);
    StringBuilder builder = new StringBuilder();
    for (String name : names) {
        builder.append(name + OsUtils.LINE_SEPARATOR);
    }
    return builder.toString();
}
 
Example #14
Source File: WorkspaceCommands.java    From geoserver-shell with MIT License 5 votes vote down vote up
@CliCommand(value = "workspace list", help = "List workspaces.")
public String list() throws Exception {
    GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
    List<String> names = reader.getWorkspaceNames();
    Collections.sort(names);
    StringBuilder builder = new StringBuilder();
    for (String name : names) {
        builder.append(name + OsUtils.LINE_SEPARATOR);
    }
    return builder.toString();
}
 
Example #15
Source File: StyleCommands.java    From geoserver-shell with MIT License 5 votes vote down vote up
@CliCommand(value = "style sld get", help = "Get the SLD of a style.")
public String getSld(
        @CliOption(key = "name", mandatory = true, help = "The name") String name,
        @CliOption(key = "workspace", mandatory = false, help = "The workspace") String workspace,
        @CliOption(key = "file", mandatory = false, help = "The output file") File file,
        @CliOption(key = "prettyprint", mandatory = false, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = "Whether to pretty print the SLD or not") boolean prettyPrint
) throws Exception {
    GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
    String sld;
    if (workspace == null) {
        sld = reader.getSLD(name);
    } else {
        sld = getSLD(name, workspace);
    }
    if (prettyPrint) {
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(new StringReader(sld));
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        sld = outputter.outputString(document);
    }
    if (file != null) {
        FileWriter writer = new FileWriter(file);
        writer.write(sld);
        writer.close();
        return file.getAbsolutePath();
    } else {
        return sld;
    }
}
 
Example #16
Source File: StyleCommands.java    From geoserver-shell with MIT License 5 votes vote down vote up
@CliCommand(value = "style list", help = "List style.")
public String list(
        @CliOption(key = "workspace", mandatory = false, help = "The workspace") String workspace
) throws Exception {
    GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
    RESTStyleList styleList = workspace != null ? this.getStyles(workspace) : reader.getStyles();
    List<String> names = styleList.getNames();
    Collections.sort(names);
    StringBuilder builder = new StringBuilder();
    for (String name : names) {
        builder.append(name + OsUtils.LINE_SEPARATOR);
    }
    return builder.toString();
}
 
Example #17
Source File: DataStoreCommands.java    From geoserver-shell with MIT License 5 votes vote down vote up
@CliCommand(value = "datastore get", help = "Get a data store.")
public String get(
        @CliOption(key = "workspace", mandatory = true, help = "The workspace") String workspace,
        @CliOption(key = "name", mandatory = true, help = "The name") String name
) throws Exception {
    GeoServerRESTReader reader = new GeoServerRESTReader(geoserver.getUrl(), geoserver.getUser(), geoserver.getPassword());
    RESTDataStore dataStore = reader.getDatastore(workspace, name);
    final String TAB = "   ";
    StringBuilder builder = new StringBuilder();
    builder.append(dataStore.getName()).append(OsUtils.LINE_SEPARATOR);
    builder.append(TAB).append("Enabled? ").append(dataStore.isEnabled()).append(OsUtils.LINE_SEPARATOR);
    builder.append(TAB).append("Description: ").append(dataStore.getDescription()).append(OsUtils.LINE_SEPARATOR);
    builder.append(TAB).append("Store Type: ").append(dataStore.getStoreType()).append(OsUtils.LINE_SEPARATOR);
    builder.append(TAB).append("Type: ").append(dataStore.getType()).append(OsUtils.LINE_SEPARATOR);
    builder.append(TAB).append("Workspace: ").append(dataStore.getWorkspaceName()).append(OsUtils.LINE_SEPARATOR);
    builder.append(TAB).append("Connection Parameters:").append(OsUtils.LINE_SEPARATOR);
    Map<String, String> params = dataStore.getConnectionParameters();
    List<Map.Entry<String, String>> connectionParamList = new LinkedList<Map.Entry<String, String>>(params.entrySet());
    Collections.sort(connectionParamList, new Comparator<Map.Entry<String,String>>() {
        @Override
        public int compare( Map.Entry<String,String> o1, Map.Entry<String,String> o2 )
        {
            return (o1.getKey()).compareTo( o2.getKey());
        }
    });
    for (Map.Entry<String, String> param : connectionParamList) {
        builder.append(TAB).append(TAB).append(param.getKey()).append(": ").append(param.getValue()).append(OsUtils.LINE_SEPARATOR);
    }
    return builder.toString();
}
 
Example #18
Source File: WorkspacesManagerServiceImpl.java    From geofence with GNU General Public License v2.0 5 votes vote down vote up
public PagingLoadResult<WorkspaceModel> getWorkspaces(int offset, int limit, String remoteURL,
    GSInstanceModel gsInstance) throws ApplicationException
{

    List<WorkspaceModel> workspacesListDTO = new ArrayList<WorkspaceModel>();
    workspacesListDTO.add(new WorkspaceModel("*"));

    if ((remoteURL != null) && !remoteURL.equals("*") && !remoteURL.contains("?"))
    {
        try
        {
            GeoServerRESTReader gsreader = new GeoServerRESTReader(remoteURL, gsInstance.getUsername(), gsInstance.getPassword());

            RESTWorkspaceList workspaces = gsreader.getWorkspaces();
            if ((workspaces != null) && !workspaces.isEmpty())
            {
                Iterator<RESTShortWorkspace> wkIT = workspaces.iterator();
                while (wkIT.hasNext())
                {
                    RESTShortWorkspace workspace = wkIT.next();

                    workspacesListDTO.add(new WorkspaceModel(workspace.getName()));
                }
            }
        }
        catch (MalformedURLException e)
        {
            logger.error(e.getLocalizedMessage(), e);
            throw new ApplicationException(e.getLocalizedMessage(), e);
        }
    }

    return new RpcPageLoadResult<WorkspaceModel>(workspacesListDTO, 0, workspacesListDTO.size());
}
 
Example #19
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Refresh workspace.
 *
 * @param workspaceName the workspace name
 */
@Override
public void refreshWorkspace(String workspaceName) {
    GeoServerRESTManager manager = GeoServerRESTManagerFactory.getManager(connection);
    if (manager != null) {
        GeoServerRESTReader reader = manager.getReader();

        if (reader != null) {
            Map<String, List<StyleWrapper>> styleMap = new LinkedHashMap<>();

            int count = 1;
            List<StyleWrapper> styleList = new ArrayList<>();

            if (workspaceName.compareTo(DEFAULT_WORKSPACE_NAME) == 0) {
                parseStyleInDefaultWorkspace(reader, count, styleList);

                styleMap.put(DEFAULT_WORKSPACE_NAME, styleList);
            } else {
                // Read styles from workspace
                parseStyleInWorkspace(reader, styleMap, count, workspaceName);
            }

            if (parentObj != null) {
                parentObj.readStylesComplete(connection, styleMap, true);
            }
        }
    }
}
 
Example #20
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #21
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parses the layer list.
 *
 * @param reader the reader
 * @param existingWorkspaceList the existing workspace list
 * @param workspaceName the workspace name
 */
private void parseLayerList(
        GeoServerRESTReader reader, List<String> existingWorkspaceList, String workspaceName) {
    Thread t1 =
            new Thread(
                    new Runnable() {
                        public void run() {
                            parseLayerListWorker(reader, existingWorkspaceList, workspaceName);
                        }
                    });
    t1.start();
}
 
Example #22
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parses the style list.
 *
 * @param reader the reader
 * @param localWorkspaceList the workspace list
 */
private void parseStyleList(GeoServerRESTReader reader, List<String> localWorkspaceList) {
    Thread t1 =
            new Thread(
                    new Runnable() {
                        public void run() {

                            Map<String, List<StyleWrapper>> styleMap = new LinkedHashMap<>();

                            int count = 1;
                            List<StyleWrapper> styleList = new ArrayList<>();

                            count = parseStyleInDefaultWorkspace(reader, count, styleList);

                            styleMap.put(DEFAULT_WORKSPACE_NAME, styleList);

                            // Read styles from workspaces
                            for (String workspaceName : localWorkspaceList) {
                                count =
                                        parseStyleInWorkspace(
                                                reader, styleMap, count, workspaceName);
                            }

                            if (parentObj != null) {
                                parentObj.readStylesComplete(connection, styleMap, false);
                            }
                        }
                    });
    t1.start();
}
 
Example #23
Source File: WorkspacesManagerServiceImpl.java    From geofence with GNU General Public License v2.0 4 votes vote down vote up
public PagingLoadResult<Layer> getLayers(int offset, int limit, String baseURL,
        GSInstanceModel gsInstance, String workspace, String service) throws ApplicationException
    {

        List<Layer> layersListDTO = new ArrayList<Layer>();
        layersListDTO.add(new Layer("*"));

        if ((baseURL != null) &&
                !baseURL.equals("*") &&
                !baseURL.contains("?") &&
                (workspace != null) &&
                (workspace.length() > 0))
        {
            try
            {
                GeoServerRESTReader gsreader = new GeoServerRESTReader(baseURL, gsInstance.getUsername(), gsInstance.getPassword());

                if (workspace.equals("*") && workspaceConfigOpts.isShowDefaultGroups() && service.equals("WMS"))
                {
                    RESTAbstractList<NameLinkElem> layerGroups = gsreader.getLayerGroups();

                    if ((layerGroups != null)) {
                        for (NameLinkElem lg : layerGroups) {
//                            RESTLayerGroup group = gsreader.getLayerGroup(lg.getName());
//                            if (group != null)
//                            {
//                                layersListDTO.add(new Layer(group.getName()));
//                            }
                            layersListDTO.add(new Layer(lg.getName()));
                        }
                    }
                }
                else
                {
                    SortedSet<String> sortedLayerNames = new TreeSet<String>();

                    if (workspace.equals("*")) { // load all layers
                        RESTAbstractList<NameLinkElem> layers = gsreader.getLayers();
                    	if (layers != null)
                    		for (NameLinkElem layerLink : layers) {
                    			sortedLayerNames.add(layerLink.getName());
                    		}
                    } else {

                        if(StringUtils.isBlank(workspace))
                            throw new ApplicationException("A workspace name is needed");

                        sortedLayerNames = getWorkspaceLayers(gsreader, workspace);
                    }
                    // return the sorted layers list
                    for (String layerName : sortedLayerNames) {
                        layersListDTO.add(new Layer(layerName));
                    }
                }
            } catch (MalformedURLException e) {
                logger.error(e.getLocalizedMessage(), e);
                throw new ApplicationException(e.getLocalizedMessage(), e);
            }
        }

        return new RpcPageLoadResult<Layer>(layersListDTO, 0, layersListDTO.size());
    }
 
Example #24
Source File: GeoServerClient.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Parses the layer.
 *
 * @param reader the reader
 * @param localWorkspaceList the local workspace list
 * @param layerMap the layer map
 * @param layers the layers
 */
private void parseLayer(
        GeoServerRESTReader reader,
        List<String> localWorkspaceList,
        Map<String, List<GeoServerLayer>> layerMap,
        RESTLayerList layers) {
    int count = 1;
    int total = layers.size();

    for (NameLinkElem featureTypeName : layers) {
        @SuppressWarnings("deprecation")
        RESTLayer layer = reader.getLayer(featureTypeName.getName());

        if (layer != null) {
            String layerName = layer.getName();
            String workspace = getLayerWorkspace(reader, localWorkspaceList, layerName);
            GeoServerLayer geoServerlayer = new GeoServerLayer();
            geoServerlayer.setLayerWorkspace(workspace);
            geoServerlayer.setLayerName(layerName);
            geoServerlayer.setConnection(connection);

            StyleWrapper styleWrapper = new StyleWrapper();
            styleWrapper.setStyle(layer.getDefaultStyle());

            String styleWorkspace = layer.getDefaultStyleWorkspace();
            styleWrapper.setWorkspace(
                    (styleWorkspace == null) ? DEFAULT_WORKSPACE_NAME : styleWorkspace);

            geoServerlayer.setStyle(styleWrapper);

            List<GeoServerLayer> layerList = layerMap.get(workspace);

            if (layerList == null) {
                layerList = new ArrayList<>();
                layerMap.put(workspace, layerList);
            }
            layerList.add(geoServerlayer);

            if (parentObj != null) {
                parentObj.readLayersProgress(connection, count, total);
            }
        }

        count++;
    }
}