Package org.geoserver.rest

Examples of org.geoserver.rest.RestletException


            isExternal = false;
            try {
                 directory = catalog.getResourceLoader().createDirectory( "data/" + coveragestore );
            }
            catch (IOException e) {
                throw new RestletException( e.getMessage(), Status.SERVER_ERROR_INTERNAL, e );
            }
        }
        final File uploadedFile = handleFileUpload(coveragestore, format, directory);
       
        // /////////////////////////////////////////////////////////////////////
        //
        // Add overviews to the Coverage
        //
        // /////////////////////////////////////////////////////////////////////
        Form form = request.getResourceRef().getQueryAsForm();
        if ("yes".equalsIgnoreCase(form.getFirstValue("overviews")) ) {
            /* TODO: Add overviews here */;
        }
           
        //create a builder to help build catalog objects
        CatalogBuilder builder = new CatalogBuilder(catalog);
        builder.setWorkspace( catalog.getWorkspaceByName( workspace ) );
       
        //create the coverage store
        CoverageStoreInfo info = catalog.getCoverageStoreByName(workspace, coveragestore);
        boolean add = false;
        if ( info == null ) {
            //create a new coverage store
            LOGGER.info("Auto-configuring coverage store: " + coveragestore);
           
            info = builder.buildCoverageStore(coveragestore);
            add = true;
        }
        else {
            //use the existing
            LOGGER.info("Using existing coverage store: " + coveragestore);
        }
       
        info.setType(coverageFormat.getName());
        if (!isExternal) {
            info.setURL("file:data/" + coveragestore + "/" + uploadedFile.getName() );
        }
        else {
            try {
                info.setURL( uploadedFile.toURL().toExternalForm());
            } catch (MalformedURLException e) {
                throw new RestletException( "url error", Status.SERVER_ERROR_INTERNAL, e );
            }
        }
       
        //add or update the datastore info
        if ( add ) {
            catalog.add( info );
        }
        else {
            catalog.save( info );
        }
       
        builder.setStore(info);
       
        //check configure parameter, if set to none to not try to configure coverage
        String configure = form.getFirstValue( "configure" );
        if ( "none".equalsIgnoreCase( configure ) ) {
            getResponse().setStatus( Status.SUCCESS_CREATED );
            return;
        }
       
        String coverage = uploadedFile.getName();
        if ( coverage.indexOf( '.') != -1 ) {
            coverage = coverage.substring( 0, coverage.indexOf( '.') );
        }
       
        try {
            AbstractGridCoverage2DReader reader =
                (AbstractGridCoverage2DReader) ((AbstractGridFormat) coverageFormat).getReader(uploadedFile.toURL());
            if ( reader == null ) {
                throw new RestletException( "Could not aquire reader for coverage.", Status.SERVER_ERROR_INTERNAL );
            }
           
            CoverageInfo cinfo = builder.buildCoverage( reader );
           
            //check if the name of the coverage was specified
            String coverageName = form.getFirstValue("coverageName");
            if ( coverageName != null ) {
                cinfo.setName( coverageName );
            }
           
            if ( !add ) {
                //update the existing
                CoverageInfo existing = catalog.getCoverageByCoverageStore(info,
                    coverageName != null ? coverageName : coverage );
                if ( existing == null ) {
                    //grab the first if there is only one
                    List<CoverageInfo> coverages = catalog.getCoveragesByCoverageStore( info);
                    if ( coverages.size() == 1 ) {
                        existing = coverages.get(0);
                    }
                    if ( coverages.size() == 0 ) {
                        //no coverages yet configured, change add flag and continue on
                        add = true;
                    }
                    else {
                        // multiple coverages, and one to configure not specified
                        throw new RestletException( "Unable to determine coverage to configure.", Status.SERVER_ERROR_INTERNAL);
                    }
                }
               
                if ( existing != null ) {
                    builder.updateCoverage(existing,cinfo);
                    catalog.save( existing );
                    cinfo = existing;
                }
            }
           
            //do some post configuration, if srs is not known or unset, transform to 4326
            if ("UNKNOWN".equals(cinfo.getSRS())) {
                //CoordinateReferenceSystem sourceCRS = cinfo.getBoundingBox().getCoordinateReferenceSystem();
                //CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326", true);
                //ReferencedEnvelope re = cinfo.getBoundingBox().transform(targetCRS, true);
                cinfo.setSRS( "EPSG:4326" );
                //cinfo.setCRS( targetCRS );
                //cinfo.setBoundingBox( re );
            }

            //add/save
            if ( add ) {
                catalog.add( cinfo );
               
                LayerInfo layerInfo = builder.buildLayer( cinfo );
                //JD: commenting this out, these sorts of edits should be handled
                // with a second PUT request on the created coverage
                /*
                String styleName = form.getFirstValue("style");
                if ( styleName != null ) {
                    StyleInfo style = catalog.getStyleByName( styleName );
                    if ( style != null ) {
                        layerInfo.setDefaultStyle( style );
                        if ( !layerInfo.getStyles().contains( style ) ) {
                            layerInfo.getStyles().add( style );
                        }
                    }
                    else {
                        LOGGER.warning( "Client specified style '" + styleName + "'but no such style exists.");
                    }
                }

                String path = form.getFirstValue( "path");
                if ( path != null ) {
                    layerInfo.setPath( path );
                }
                */

                catalog.add(layerInfo);
            }
            else {
                catalog.save( cinfo );
               
                //TODO: update the layers pointing at this coverage
            }
           
            //poach the coverage store data format
            DataFormat df = new CoverageStoreResource(getContext(),request,response,catalog)
                .createXMLFormat(request, response);
            response.setEntity(df.toRepresentation(info));
            response.setStatus(Status.SUCCESS_CREATED);
        }
        catch( Exception e ) {
            throw new RestletException( "Error auto-configuring coverage", Status.SERVER_ERROR_INTERNAL, e );
        }
    }
View Full Code Here


        if ( format instanceof SLDFormat ) {
            try {
                return sinfo.getStyle();
            }
            catch (IOException e) {
                throw new RestletException( "", Status.SERVER_ERROR_INTERNAL, e );
            }
        }
       
        return sinfo;
    }
View Full Code Here

           
            if ( layer != null ) {
                StyleInfo existing = catalog.getStyleByName( style.getName() );
                if ( existing == null ) {
                    //TODO: add a new style to catalog
                    throw new RestletException( "No such style: " + style.getName(), Status.CLIENT_ERROR_NOT_FOUND );
                }
               
                LayerInfo l = catalog.getLayerByName( layer );
                l.getStyles().add( existing );
               
                //check for default
                String def = getRequest().getResourceRef().getQueryAsForm().getFirstValue("default");
                if ( "true".equals( def ) ) {
                    l.setDefaultStyle( existing );
                }
                catalog.save(l);
                LOGGER.info( "POST style " + style.getName() + " to layer " + layer);
            }
            else {
                catalog.add( style  );
                LOGGER.info( "POST style " + style.getName() );
            }

            return style.getName();
        }
        else if ( object instanceof Style ) {
            Style style = (Style) object;
           
            //figure out the name of the new style, first check if specified directly
            String name = getRequest().getResourceRef().getQueryAsForm().getFirstValue( "name");
           
            if ( name == null ) {
                //infer name from sld
                name = style.getName();
            }
           
            if ( name == null ) {
                throw new RestletException( "Style must have a name.", Status.CLIENT_ERROR_BAD_REQUEST );
            }
           
            //ensure that the style does not already exist
            if ( catalog.getStyleByName( name ) != null ) {
                throw new RestletException( "Style " + name + " already exists.", Status.CLIENT_ERROR_FORBIDDEN  );
            }
           
            //serialize the style out into the data directory
            GeoServerResourceLoader loader = catalog.getResourceLoader();
            File f;
            try {
                f = loader.find( "styles/" +  name + ".sld" );
            }
            catch (IOException e) {
                throw new RestletException( "Error looking up file", Status.SERVER_ERROR_INTERNAL, e );
            }
           
            if ( f != null ) {
                String msg = "SLD file " + name + ".sld already exists.";
                throw new RestletException( msg, Status.CLIENT_ERROR_FORBIDDEN);
            }
           
            //TODO: have the writing out of the style delegate to ResourcePool.writeStyle()
            try {
                f = loader.createFile( "styles/" + name + ".sld") ;
               
                //serialize the file to the styles directory
                BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream ( f ) );
               
                SLDFormat format = new SLDFormat(true);
                format.toRepresentation(style).write(out);
               
                out.flush();
                out.close();
            }
            catch (IOException e) {
                throw new RestletException( "Error creating file", Status.SERVER_ERROR_INTERNAL, e );
            }
           
            //create a style info object
            StyleInfo sinfo = catalog.getFactory().createStyle();
            sinfo.setName( name );
View Full Code Here

        StyleInfo s = catalog.getStyleByName(style);
       
        //ensure that no layers reference the style
        List<LayerInfo> layers = catalog.getLayers(s);
        if ( !layers.isEmpty() ) {
            throw new RestletException( "Can't delete style referenced by existing layers.", Status.CLIENT_ERROR_FORBIDDEN );
        }
       
        catalog.remove( s );
       
        //check purge parameter to determine if the underlying file
View Full Code Here

        }
       
        if ( workspace != null ) {
            //ensure it exists
            if ( !"default".equals( workspace) && catalog.getWorkspaceByName( workspace ) == null ) {
                throw new RestletException( "No such workspace: " + workspace, Status.CLIENT_ERROR_NOT_FOUND );
            }
        }
       
        return new WorkspaceResource( null, request, response, catalog );
    }
View Full Code Here

            NamespaceInfo ns = catalog.getNamespaceByPrefix( workspace );
            if ( ns != null ) {
                return catalog.getFeatureTypeByName(ns,featureType);
            }

            throw new RestletException( "", Status.CLIENT_ERROR_NOT_FOUND );
        }

        LOGGER.fine( "GET feature type" + datastore + "," + featureType );
        DataStoreInfo ds = catalog.getDataStoreByName(workspace, datastore);
        return catalog.getFeatureTypeByDataStore( ds, featureType );
View Full Code Here

        FeatureTypeInfo featureType = (FeatureTypeInfo) object;
        
        //ensure the store matches up
        if ( featureType.getStore() != null ) {
            if ( !dataStore.equals( featureType.getStore().getName() ) ) {
                throw new RestletException( "Expected datastore " + dataStore +
                " but client specified " + featureType.getStore().getName(), Status.CLIENT_ERROR_FORBIDDEN );
            }
        }
        else {
            featureType.setStore( catalog.getDataStoreByName( workspace, dataStore ) );
        }
       
        //ensure workspace/namespace matches up
        if ( featureType.getNamespace() != null ) {
            if ( !workspace.equals( featureType.getNamespace().getPrefix() ) ) {
                throw new RestletException( "Expected workspace " + workspace +
                    " but client specified " + featureType.getNamespace().getPrefix(), Status.CLIENT_ERROR_FORBIDDEN );
            }
        }
        else {
            featureType.setNamespace( catalog.getNamespaceByPrefix( workspace ) );
View Full Code Here

        else {
            WorkspaceInfo original = catalog.getWorkspaceByName( ws );
           
            //ensure this is not a name change
            if ( workspace.getName() != null && !workspace.getName().equals( original.getName() ) ) {
                throw new RestletException( "Can't change the name of a workspace.", Status.CLIENT_ERROR_FORBIDDEN );
            }
           
            new CatalogBuilder(catalog).updateWorkspace( original, workspace );
            catalog.save( original );
        }
View Full Code Here

    protected void handleObjectDelete() throws Exception {
        String workspace = (String) getRequest().getAttributes().get( "workspace");
        WorkspaceInfo ws = catalog.getWorkspaceByName( workspace );
       
        if ( !catalog.getStoresByWorkspace(ws, StoreInfo.class).isEmpty() ) {
            throw new RestletException( "Workspace not empty", Status.CLIENT_ERROR_FORBIDDEN );
        }
       
        //check for "linked" workspace
        NamespaceInfo ns = catalog.getNamespaceByPrefix( ws.getName() );
        if ( ns != null ) {
            if ( !catalog.getFeatureTypesByNamespace( ns ).isEmpty() ) {
                throw new RestletException( "Namespace for workspace not empty.", Status.CLIENT_ERROR_FORBIDDEN );
            }
            catalog.remove( ns );
        }
       
        catalog.remove( ws );
View Full Code Here

        String workspace = getAttribute("workspace");
        String coveragestore = getAttribute("coveragestore");
       
        CoverageStoreInfo cs = catalog.getCoverageStoreByName(workspace, coveragestore);
        if ( !catalog.getCoveragesByCoverageStore(cs).isEmpty() ) {
            throw new RestletException( "coveragestore not empty", Status.CLIENT_ERROR_UNAUTHORIZED);
        }
        catalog.remove( cs );
       
        LOGGER.info( "DELETE coverage store " + workspace + "," + coveragestore );
    }
View Full Code Here

TOP

Related Classes of org.geoserver.rest.RestletException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.