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) {
throw new ServiceException(e);
}
return request;
}