@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public GetLegendGraphicRequest read(Object req, Map kvp, Map rawKvp) throws Exception {
GetLegendGraphicRequest request = (GetLegendGraphicRequest) super.read(req, kvp, rawKvp);
request.setRawKvp(rawKvp);
if (request.getVersion() == null || request.getVersion().length() == 0) {
String version = (String) rawKvp.get("WMTVER");
if (version == null) {
version = wms.getVersion();
}
request.setVersion(version);
}
// Fix for http://jira.codehaus.org/browse/GEOS-710
// Since at the moment none of the other request do check the version
// numbers, we
// disable this check for the moment, and wait for a proper fix once the
// we support more than one version of WMS/WFS specs
// if (!GetLegendGraphicRequest.SLD_VERSION.equals(version)) {
// throw new WmsException("Invalid SLD version number \"" + version
// + "\"");
// }
final String layer = (String) rawKvp.get("LAYER");
final boolean strict = rawKvp.containsKey("STRICT") ? Boolean.valueOf((String) rawKvp
.get("STRICT")) : request.isStrict();
request.setStrict(strict);
if (strict && layer == null) {
throw new ServiceException("LAYER parameter not present for GetLegendGraphic",
"LayerNotDefined");
}
if (strict && request.getFormat() == null) {
throw new ServiceException("Missing FORMAT parameter for GetLegendGraphic",
"MissingFormat");
}
MapLayerInfo mli = null;
if (layer != null) {
LayerInfo layerInfo = wms.getLayerByName(layer);
if (layerInfo == null) {
throw new ServiceException(layer + " layer does not exist.");
}
mli = new MapLayerInfo(layerInfo);
try {
if (layerInfo.getType() == Type.VECTOR) {
FeatureType featureType = mli.getFeature().getFeatureType();
request.setLayer(featureType);
} else if (layerInfo.getType() == Type.RASTER) {
CoverageInfo coverageInfo = mli.getCoverage();
// it much safer to wrap a reader rather than a coverage in most cases, OOM can
// occur otherwise
final AbstractGridCoverage2DReader reader;
reader = (AbstractGridCoverage2DReader) coverageInfo.getGridCoverageReader(
new NullProgressListener(), GeoTools.getDefaultHints());
final SimpleFeatureCollection feature;
feature = FeatureUtilities.wrapGridCoverageReader(reader, null);
request.setLayer(feature.getSchema());
}
} catch (IOException e) {
throw new ServiceException(e);
} catch (NoSuchElementException ne) {
throw new ServiceException(new StringBuffer(layer)
.append(" layer does not exists.").toString(), ne);
} catch (Exception te) {
throw new ServiceException("Can't obtain the schema for the required layer.", te);
}
}
if (request.getFormat() == null) {
request.setFormat(GetLegendGraphicRequest.DEFAULT_FORMAT);
}
if (null == wms.getLegendGraphicOutputFormat(request.getFormat())) {
throw new ServiceException(new StringBuffer("Invalid graphic format: ").append(
request.getFormat()).toString(), "InvalidFormat");
}
try {
parseOptionalParameters(request, mli, rawKvp);
} catch (IOException e) {