Changeset 379

Show
Ignore:
Timestamp:
09/03/08 22:18:54 (4 months ago)
Author:
arneke
Message:

Applied formatting to getcapabilities code, added exceptions where necessary.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/geowebcache/src/main/java/org/geowebcache/service/wms/WMSService.java

    r378 r379  
    3434import org.apache.commons.logging.Log; 
    3535import org.apache.commons.logging.LogFactory; 
    36 import org.geotools.data.ows.WMSCapabilities; 
    37 import org.geotools.data.wms.WebMapServer; 
    3836import org.geowebcache.GeoWebCacheException; 
    3937import org.geowebcache.layer.SRS; 
     
    5654    private static Log log = LogFactory.getLog(org.geowebcache.service.wms.WMSService.class); 
    5755 
    58         private List configs; 
    59  
    60         private String caps; 
     56    // The following two are only needed for the  
     57    // temporary getcapabilities code 
     58    private List<Configuration> getCapConfigs; 
     59 
     60    private String getCapsStr; 
    6161 
    6262    public WMSService() { 
     
    192192    } 
    193193 
    194     private String getCapabilitiesHeader() { 
    195         String wms = fetchOrigionalWMSCapabilitiesDocument(); 
    196         int split = wms.indexOf("<VendorSpecificCapabilities/>"); 
    197         if( split != -1 ){ 
    198                 // we have an empty VendorSpecificCapabilities to fill in... 
    199                 String header = wms.substring(0,split); 
    200                 return header+"\n<VendorSpecificCapabilities>"; 
    201         } 
    202         split = wms.indexOf("</VendorSpecificCapabilities>"); 
    203         if( split != -1 ){ 
    204                 // we have an existing VendorSpecificCapabilities to add to 
    205                 String header = wms.substring(0,split);                  
    206         } 
    207         // look for <UserDefinedSymbolization .. VendorSpecificCapabilities goes before this element 
    208         split = wms.indexOf("<UserDefinedSymbolization"); 
    209         if( split ==-1 ){ 
    210                 // look for <Layer> ... VendorSpecificParameters goes before this element 
    211                 split = wms.indexOf("<Layer"); 
    212         } 
    213         String header = wms.substring(0,split); 
    214          
    215         return header +"\n<VendorSpecificCapabilities>"; 
    216     } 
    217  
    218     private String getCapabilitiesFooter() { 
    219         //return "\n</VendorSpecificCapabilities>" + "\n</WMT_MS_Capabilities>"; 
    220         String wms = fetchOrigionalWMSCapabilitiesDocument(); 
    221         int split = wms.indexOf("<VendorSpecificCapabilities/>"); 
    222         if( split != -1 ){ 
    223                 // we have an empty VendorSpecificCapabilities to fill in... 
    224                 String footer = wms.substring(split+29); 
    225                 return "\n</VendorSpecificCapabilities>" + footer; 
    226         } 
    227         split = wms.indexOf("<VendorSpecificCapabilities/>"); 
    228         if( split != -1 ){ 
    229                 // we have an existing VendorSpecificCapabilities to add to 
    230                 String footer = wms.substring(split+28); 
    231                 return "\n</VendorSpecificCapabilities>" + footer; 
    232         } 
    233         // look for <UserDefinedSymbolization .. VendorSpecificCapabilities goes before this element 
    234         split = wms.indexOf("<UserDefinedSymbolization"); 
    235         if( split ==-1 ){ 
    236                 // look for <Layer> ... VendorSpecificParameters goes before this element 
    237                 split = wms.indexOf("<Layer"); 
    238         } 
    239         String footer = wms.substring(split);            
    240         return "\n</VendorSpecificCapabilities>" + footer; 
     194    private String getCapabilitiesHeader() throws GeoWebCacheException { 
     195        String wms = fetchOrigionalWMSCapabilitiesDocument(); 
     196 
     197        if (wms == null) { 
     198            throw new GeoWebCacheException( 
     199                    "Unable to retrieve original WMS Capabilities document"); 
     200        } 
     201 
     202        int split = wms.indexOf("<VendorSpecificCapabilities/>"); 
     203        if (split != -1) { 
     204            // we have an empty VendorSpecificCapabilities to fill in... 
     205            String header = wms.substring(0, split); 
     206            return header + "\n<VendorSpecificCapabilities>"; 
     207        } 
     208        split = wms.indexOf("</VendorSpecificCapabilities>"); 
     209         
     210        // Was never read anyway 
     211        //if (split != -1) { 
     212            // we have an existing VendorSpecificCapabilities to add to 
     213        //    String header = wms.substring(0, split); 
     214        //} 
     215         
     216        // look for <UserDefinedSymbolization .. VendorSpecificCapabilities goes 
     217        // before this element 
     218        split = wms.indexOf("<UserDefinedSymbolization"); 
     219        if (split == -1) { 
     220            // look for <Layer> ... VendorSpecificParameters goes before this 
     221            // element 
     222            split = wms.indexOf("<Layer"); 
     223        } 
     224        String header = wms.substring(0, split); 
     225 
     226        return header + "\n<VendorSpecificCapabilities>"; 
     227    } 
     228 
     229    private String getCapabilitiesFooter() throws GeoWebCacheException { 
     230        // return "\n</VendorSpecificCapabilities>" + 
     231        // "\n</WMT_MS_Capabilities>"; 
     232        String wms = fetchOrigionalWMSCapabilitiesDocument(); 
     233        int split = wms.indexOf("<VendorSpecificCapabilities/>"); 
     234        if (split != -1) { 
     235            // we have an empty VendorSpecificCapabilities to fill in... 
     236            String footer = wms.substring(split + 29); 
     237            return "\n</VendorSpecificCapabilities>" + footer; 
     238        } 
     239        split = wms.indexOf("<VendorSpecificCapabilities/>"); 
     240        if (split != -1) { 
     241            // we have an existing VendorSpecificCapabilities to add to 
     242            String footer = wms.substring(split + 28); 
     243            return "\n</VendorSpecificCapabilities>" + footer; 
     244        } 
     245        // look for <UserDefinedSymbolization .. VendorSpecificCapabilities goes 
     246        // before this element 
     247        split = wms.indexOf("<UserDefinedSymbolization"); 
     248        if (split == -1) { 
     249            // look for <Layer> ... VendorSpecificParameters goes before this 
     250            // element 
     251            split = wms.indexOf("<Layer"); 
     252        } 
     253        String footer = wms.substring(split); 
     254        return "\n</VendorSpecificCapabilities>" + footer; 
    241255    } 
    242256 
    243257    /** 
    244      * Fetch the original WMS capabilities document (we will add our vendor specific 
    245      * parameters here). 
     258     * Fetch the original WMS capabilities document (we will add our vendor 
     259     * specific parameters here). 
    246260     * <p> 
    247261     * Currently this is returned as a String; in the future we can make use of 
    248      * the GeoTools WMSCapabilities data structure (and strip out any 
    249      * layers that are not mentioned explicitly). 
     262     * the GeoTools WMSCapabilities data structure (and strip out any layers 
     263     * that are not mentioned explicitly). 
    250264     *  
    251      * @return The origional WMS capabilities document prior to processing 
     265     * @return The original WMS capabilities document prior to processing 
     266     * @throws GeoWebCacheException  
    252267     */ 
    253     synchronized String fetchOrigionalWMSCapabilitiesDocument(){ 
    254         if( caps != null ){ 
    255                 return caps; 
    256         } 
    257         StringBuffer buf = new StringBuffer(); 
    258         if (configs == null ){ 
    259                 return "sad"; 
    260         } 
    261          Iterator configIter = configs.iterator(); 
    262          CONFIG: while (configIter.hasNext()) { 
    263              Map<String, TileLayer> configLayers = null; 
    264              Configuration config = (Configuration) configIter.next(); 
    265              try { 
    266                  configLayers = config.getTileLayers(); 
    267              } catch (GeoWebCacheException gwce) { 
    268                  log.error(gwce.getMessage()); 
    269                  log.error("Failed to add layers from "+ config.getIdentifier()); 
    270              } 
    271              if (configLayers != null && configLayers.size() > 0) { 
    272                  LAYER: for( TileLayer layer : configLayers.values() ){ 
    273                          if( !(layer instanceof WMSLayer)){ 
    274                                  continue; // skip! 
    275                          } 
    276                          WMSLayer wmsLayer = (WMSLayer) layer; 
    277                          WMSURL: for( String url : wmsLayer.getWMSurl() ){ 
    278                                  try { 
    279                                          URL capabilitiesURL = new URL( url+"?REQUEST=GetCapabilities&SERVICE=WMS&VESION=1.1.0"); 
    280                                                  URLConnection connection = capabilitiesURL.openConnection(); 
    281                                                  InputStream input = connection.getInputStream(); 
    282                                                  InputStreamReader reader = new InputStreamReader( input ); 
    283                                                  BufferedReader process = new BufferedReader( reader ); 
    284                                                   
    285                                                  buf = new StringBuffer(); 
    286                                                  String line; 
    287                                                  while( (line = process.readLine()) != null ){ 
    288                                                          buf.append( line ); 
    289                                                          buf.append("\n"); 
    290                                                  } 
    291                                                  if( buf.length() != 0 ){ 
    292                                                          break CONFIG; // we managed to read a capabilities into buf 
    293                                                  } 
    294                                                  /*   
    295                                                      // TODO only use the parts of the capabilities file that 
    296                                                          // are mentioned in our configuration! 
    297                                                            
    298                                                          WebMapServer wms = new WebMapServer(capabilitiesURL); 
    299                                                          WMSCapabilities capabilities = wms.getCapabilities();                                                                                           
    300                                                  */ 
    301                                  } 
    302                                  catch( Throwable notConnected ){ 
    303                                          // continue WMSURL 
    304                                  }                                
    305                          } 
    306                  } 
    307              } else { 
    308                  log.error("Configuration " + config.getIdentifier()+ " contained no layers."); 
    309              } 
    310          } 
    311          caps = buf.toString(); 
    312          return caps; 
    313     } 
    314  
    315     public void setConfig(List configs) { 
    316         this.configs = configs; 
    317     } 
     268    synchronized String fetchOrigionalWMSCapabilitiesDocument() throws GeoWebCacheException { 
     269        if (getCapsStr != null) { 
     270            return getCapsStr; 
     271        } 
     272        StringBuffer buf = new StringBuffer(); 
     273         
     274        if (getCapConfigs == null) { 
     275            throw new GeoWebCacheException("No configuration object available" + 
     276                        " to use for WMS Capabilities"); 
     277        } 
     278        Iterator<Configuration> configIter = getCapConfigs.iterator(); 
     279        CONFIG: while (configIter.hasNext()) { 
     280            Map<String, TileLayer> configLayers = null; 
     281            Configuration config = configIter.next(); 
     282            try { 
     283                configLayers = config.getTileLayers(); 
     284            } catch (GeoWebCacheException gwce) { 
     285                log.error(gwce.getMessage()); 
     286                log.error("Failed to add layers from "+ config.getIdentifier()); 
     287            } 
     288            if (configLayers != null && configLayers.size() > 0) { 
     289                for (TileLayer layer : configLayers.values()) { 
     290                    if (!(layer instanceof WMSLayer)) { 
     291                        continue; // skip! 
     292                    } 
     293                    WMSLayer wmsLayer = (WMSLayer) layer; 
     294                    for (String url : wmsLayer.getWMSurl()) { 
     295                        try { 
     296                            URL capabilitiesURL = new URL( 
     297                                    url+ "?REQUEST=GetCapabilities&SERVICE=WMS&VESION=1.1.0"); 
     298                            URLConnection connection = capabilitiesURL.openConnection(); 
     299                            InputStream input = connection.getInputStream(); 
     300                            InputStreamReader reader = new InputStreamReader(input); 
     301                            BufferedReader process = new BufferedReader(reader); 
     302 
     303                            buf = new StringBuffer(); 
     304                            String line; 
     305                            while ((line = process.readLine()) != null) { 
     306                                buf.append(line); 
     307                                buf.append("\n"); 
     308                            } 
     309                            if (buf.length() != 0) { 
     310                                break CONFIG; // we managed to read a 
     311                                              // capabilities into buf 
     312                            } 
     313                            /* 
     314                             * // TODO only use the parts of the capabilities 
     315                             * file that // are mentioned in our configuration! 
     316                             *  
     317                             * WebMapServer wms = new 
     318                             * WebMapServer(capabilitiesURL); WMSCapabilities 
     319                             * capabilities = wms.getCapabilities(); 
     320                             */ 
     321                        } catch (Throwable notConnected) { 
     322                            // continue WMSURL 
     323                        } 
     324                    } 
     325                } 
     326            } else { 
     327                log.error("Configuration " + config.getIdentifier() 
     328                        + " contained no layers."); 
     329            } 
     330        } 
     331        getCapsStr = buf.toString(); 
     332        return getCapsStr; 
     333    } 
     334 
     335    public void setConfig(List<Configuration> configs) { 
     336        this.getCapConfigs = configs; 
     337    } 
     338 
    318339    /** 
    319340     *  
     
    323344    private String getTileSets(TileLayer tl) throws GeoWebCacheException { 
    324345        String ret = ""; 
    325          
     346 
    326347        List<MimeType> mimeList = tl.getMimeTypes(); 
    327348        String strStyles = tl.getStyles(); 
     
    331352 
    332353        Iterator<Grid> iter = tl.getGrids().values().iterator(); 
    333         while(iter.hasNext()) { 
     354        while (iter.hasNext()) { 
    334355            Grid grid = iter.next(); 
    335              
     356 
    336357            // These should be adjusted bounds! 
    337358            String[] strBounds = doublesToStrings(grid.getBounds().coords); 
     
    341362            for (MimeType mime : mimeList) { 
    342363                String strFormat = mime.getFormat(); 
    343                 ret += getTileSet(strName, grid.getSRS().toString(), strBounds, strStyles, 
    344                         strResolutions, strFormat); 
     364                ret += getTileSet(strName, grid.getSRS().toString(), strBounds, 
     365                        strStyles, strResolutions, strFormat); 
    345366            } 
    346367        } 
     
    352373            String[] strBounds, String strStyles, String strResolutions, 
    353374            String strFormat) { 
    354         return "\n<TileSet>" + "<SRS>" + strSRS + "</SRS>" 
    355                 + "<BoundingBox srs=\"" + strSRS + "\"" + " minx=\"" 
    356                 + strBounds[0] + "\"" + " miny=\"" + strBounds[1] + "\"" 
    357                 + " maxx=\"" + strBounds[2] + "\"" + " maxy=\"" + strBounds[3] 
    358                 + "\" />" + "<Resolutions>" + strResolutions + "</Resolutions>" 
    359                 + "<Width>256</Width>" + "<Height>256</Height>" + "<Format>" 
    360                 + strFormat + "</Format>" + "<Layers>" + strName + "</Layers>" 
    361                 + "<Styles>" + strStyles + "</Styles>" + "</TileSet>"; 
     375        return "\n<TileSet>"  
     376            + "<SRS>" + strSRS + "</SRS>" 
     377            + "<BoundingBox srs=\"" + strSRS + "\"" + " minx=\"" 
     378            + strBounds[0] + "\"" + " miny=\"" + strBounds[1] + "\"" 
     379            + " maxx=\"" + strBounds[2] + "\"" + " maxy=\"" + strBounds[3] 
     380            + "\" />"  
     381            + "<Resolutions>" + strResolutions + "</Resolutions>" 
     382            + "<Width>256</Width>"  
     383            + "<Height>256</Height>"  
     384            + "<Format>" + strFormat + "</Format>"  
     385            + "<Layers>" + strName + "</Layers>" 
     386            + "<Styles>" + strStyles + "</Styles>"  
     387            + "</TileSet>"; 
    362388    } 
    363389