Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SLD package extension in StyleResource. Added docs, unit tests a…
…nd code refactor
- Loading branch information
Showing
7 changed files
with
254 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/src/main/java/org/geoserver/catalog/SLDPackageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,122 @@ | |||
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved | |||
* (c) 2001 - 2014 OpenPlans | |||
* This code is licensed under the GPL 2.0 license, available at the root | |||
* application directory. | |||
*/ | |||
package org.geoserver.catalog; | |||
|
|||
import com.google.common.io.Files; | |||
import org.apache.commons.io.FileUtils; | |||
import org.geotools.map.StyleLayer; | |||
import org.geotools.styling.*; | |||
import org.geotools.util.Version; | |||
import org.geotools.util.logging.Logging; | |||
import org.xml.sax.EntityResolver; | |||
|
|||
import java.io.*; | |||
import java.util.List; | |||
import java.util.logging.Logger; | |||
import java.util.zip.ZipEntry; | |||
import java.util.zip.ZipOutputStream; | |||
|
|||
/** | |||
* SLD package style handler. | |||
* | |||
* @author Jose García | |||
* | |||
*/ | |||
public class SLDPackageHandler extends StyleHandler { | |||
|
|||
static Logger LOGGER = Logging.getLogger(SLDPackageHandler.class); | |||
|
|||
public static final String FORMAT = "zip"; | |||
public static final String MIMETYPE = "application/zip"; | |||
|
|||
private SLDHandler sldHandler; | |||
|
|||
protected SLDPackageHandler(SLDHandler sldHandler) { | |||
super("ZIP", FORMAT); | |||
this.sldHandler = sldHandler; | |||
} | |||
|
|||
@Override | |||
public String mimeType(Version version) { | |||
return MIMETYPE; | |||
} | |||
|
|||
|
|||
@Override | |||
public StyledLayerDescriptor parse(Object input, Version version, ResourceLocator resourceLocator, EntityResolver entityResolver) throws IOException { | |||
File sldFile = null; | |||
try | |||
{ | |||
sldFile = unzipSldPackage(input); | |||
return sldHandler.parse(sldFile, version, resourceLocator, entityResolver); | |||
} | |||
finally | |||
{ | |||
if (sldFile != null) FileUtils.deleteQuietly(sldFile.getParentFile()); | |||
} | |||
} | |||
|
|||
|
|||
@Override | |||
public void encode(StyledLayerDescriptor sld, Version version, boolean pretty, OutputStream output) throws IOException { | |||
ZipOutputStream zos = new ZipOutputStream(output); | |||
|
|||
try { | |||
String sldName = (sld.getName() != null)?sld.getName():"style"; | |||
zos.putNextEntry(new ZipEntry(sldName + ".sld")); | |||
|
|||
OutputStream sldOutputStream = new ByteArrayOutputStream(); | |||
sldHandler.encode(sld, version, pretty, sldOutputStream); | |||
|
|||
zos.write(((ByteArrayOutputStream) sldOutputStream).toByteArray()); | |||
zos.closeEntry(); | |||
} | |||
finally { | |||
zos.close(); | |||
} | |||
|
|||
} | |||
|
|||
@Override | |||
public List<Exception> validate(Object input, Version version, EntityResolver entityResolver) throws IOException { | |||
File sldFile = null; | |||
try | |||
{ | |||
sldFile = unzipSldPackage(input); | |||
return sldHandler.validate(input, version, entityResolver); | |||
} | |||
finally | |||
{ | |||
if (sldFile != null) FileUtils.deleteQuietly(sldFile.getParentFile()); | |||
} | |||
} | |||
|
|||
/** | |||
* Unzips a SLD package to a temporal folder, returning the SLD file path. | |||
* | |||
* @param input | |||
* @return | |||
* @throws IOException | |||
*/ | |||
private File unzipSldPackage(Object input) throws IOException { | |||
File myTempDir = Files.createTempDir(); | |||
|
|||
org.geoserver.data.util.IOUtils.decompress((InputStream) input, myTempDir); | |||
|
|||
File[] files = myTempDir.listFiles(new FilenameFilter() { | |||
public boolean accept(File dir, String name) { | |||
return name.toLowerCase().endsWith(".sld"); | |||
} | |||
}); | |||
|
|||
if (files.length != 1) { | |||
throw new IOException("No SLD file"); | |||
} | |||
|
|||
return files[0]; | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.