Skip to content

Commit

Permalink
Allow GWC publishing formats to be contributed by a properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Roldan committed Jun 19, 2015
1 parent aa0a3d1 commit 33542ae
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
64 changes: 64 additions & 0 deletions src/gwc/src/main/java/org/geoserver/gwc/GWC.java
Expand Up @@ -9,11 +9,14 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.getRootCause;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterators.forEnumeration;
import static com.google.common.collect.Lists.newArrayList;
import static org.geowebcache.grid.GridUtil.findBestMatchingGrid;
import static org.geowebcache.seed.GWCTask.TYPE.TRUNCATE;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -22,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
Expand All @@ -39,6 +43,7 @@
import org.geoserver.catalog.LayerGroupInfo;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.NamespaceInfo;
import org.geoserver.catalog.PublishedType;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.StyleInfo;
import org.geoserver.gwc.config.GWCConfig;
Expand Down Expand Up @@ -121,6 +126,7 @@
import org.springframework.context.ApplicationContextAware;

import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -2059,4 +2065,62 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
this.applicationContext = applicationContext;
}

/**
* Returns the list of {@link MimeType#getFormat() MIME Type formats} advertised as valid for
* caching for the given type of published kind of layer.
* <p>
* Handles the case where some tile formats may be appropriate for vector layers but not for
* raster layers, or vice-versa.
* <p>
* Loads all resources in the classpath named
* {@code /org/geoserver/gwc/advertised_formats.properties} so other modules can contribute
* advertised formats without introducing unneeded dependencies.
* <p>
* {@code /org/geoserver/gwc/advertised_formats.properties} has entries for the following keys,
* whose values are a comma separated list of GWC MIME format names: {@code formats.vector},
* {@code formats.raster}, and {@code formats.group}
*
* @param type the kind of geoserver published resource the tile layer is tied to.
* @return the set of advertised mime types for the given kind of tile layer origin
*/
public Set<String> getAdvertisedCachedFormats(final PublishedType type) {

final String resourceName = "org/geoserver/gwc/advertised_formats.properties";
final String formatsKey;

switch (type) {
case VECTOR: // local vector layer
case REMOTE: // remote WFS
formatsKey = "formats.vector";
break;
case RASTER: // local raster layer
case WMS: // remote WMS raster
formatsKey = "formats.raster";
break;
case GROUP:
formatsKey = "formats.layergroup";
break;
default:
throw new IllegalArgumentException("Unknown published type: " + type);
}
Set<String> formats = new TreeSet<String>();
try {
ClassLoader classLoader = getClass().getClassLoader();
ArrayList<URL> urls = newArrayList(forEnumeration(classLoader.getResources(resourceName)));
for (URL url : urls) {
Properties props = new Properties();
props.load(url.openStream());
String commaSeparatedFormats = props.getProperty(formatsKey);
if (commaSeparatedFormats != null) {
List<String> splitToList = Splitter.on(",").omitEmptyStrings().trimResults()
.splitToList(commaSeparatedFormats);
formats.addAll(splitToList);
}
}
} catch (IOException e) {
throw Throwables.propagate(e);
}
return formats;
}

}
@@ -0,0 +1,10 @@
#
# A resource meant to contribute GWC tile encoding formats to the
# configuration UI through the org.geoserver.gwc.GWC.getAdvertisedCachedFormats(PublishedType)
# method, so that format options can be contributed by other modules
# without introducing unneeded dependencies. Multiple resources names like this
# one can exist in other jar files and GWC will take care of loading them
#
formats.vector = image/png, image/png8, image/jpeg, image/gif
formats.raster = image/png, image/png8, image/jpeg, image/gif
formats.layergroup = image/png, image/png8, image/jpeg, image/gif
Expand Up @@ -39,6 +39,8 @@
import org.geoserver.catalog.LayerGroupInfo;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.MetadataMap;
import org.geoserver.catalog.PublishedInfo;
import org.geoserver.catalog.PublishedType;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.impl.ModificationProxy;
import org.geoserver.gwc.ConfigurableBlobStore;
Expand All @@ -58,6 +60,7 @@
import org.geowebcache.storage.blobstore.memory.CacheProvider;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

/**
* Edit panel for a {@link GeoServerTileLayerInfo} (used to edit caching options for both
Expand Down Expand Up @@ -140,7 +143,7 @@ class GeoServerTileLayerEditor extends FormComponentPanel<GeoServerTileLayerInfo
* @param tileLayerModel must be a {@link GeoServerTileLayerInfoModel}
*/
public GeoServerTileLayerEditor(final String id,
final IModel<? extends CatalogInfo> layerModel,
final IModel<? extends PublishedInfo> layerModel,
final IModel<GeoServerTileLayerInfo> tileLayerModel) {
super(id);
checkArgument(tileLayerModel instanceof GeoServerTileLayerInfoModel);
Expand All @@ -150,7 +153,7 @@ public GeoServerTileLayerEditor(final String id,
final GWC mediator = GWC.get();
final IModel<String> createTileLayerLabelModel;

final CatalogInfo info = layerModel.getObject();
final PublishedInfo info = layerModel.getObject();
final GeoServerTileLayerInfo tileLayerInfo = tileLayerModel.getObject();

if (info instanceof LayerInfo) {
Expand Down Expand Up @@ -254,8 +257,8 @@ public GeoServerTileLayerEditor(final String id,
cacheFormats.setLabel(new ResourceModel("cacheFormats"));
configs.add(cacheFormats);

final List<String> formats = Arrays.asList("image/png", "image/png8", "image/jpeg",
"image/gif", "application/json;type=topojson");
final List<String> formats;
formats = Lists.newArrayList(GWC.get().getAdvertisedCachedFormats(info.getType()));

ListView<String> cacheFormatsList = new ListView<String>("cacheFormats", formats) {
private static final long serialVersionUID = 1L;
Expand Down

0 comments on commit 33542ae

Please sign in to comment.