protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Ignore path. Allows client to set any download file name
// Parse format
Format format = null;
{
String formatStr = request.getParameter("format");
if( null == formatStr ) {
format = Format.GEOJSON;
} else {
for(Format f : Format.values()){
if( f.matches(formatStr) ){
format = f;
}
}
}
if( null == format ) {
Exception e = new Exception("Unknown format");
reportError(response,HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e);
return;
}
logger.debug("Export Format: "+format.name());
}
// Parse filter
Filter filter = null;
{
String filterStr = request.getParameter("filter");
if( null == filterStr ) {
filter = Filter.ALL;
} else {
for(Filter f : Filter.values()){
if( f.matches(filterStr) ){
filter = f;
}
}
}
if( null == filter ) {
Exception e = new Exception("Unknown filter");
reportError(response,HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e);
return;
}
logger.debug("Export Filter: "+filter.name());
}
// Parse method
Method method = null;
{
String methodStr = request.getParameter("method");
if( null != methodStr ) {
for(Method m : Method.values()){
if( m.matches(methodStr) ){
method = m;
}
}
}
if( null == method ) {
Exception e = new Exception("Unknown method");
reportError(response,HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e);
return;
}
logger.debug("Export Method: "+method.name());
}
// Parse identifier
String identifier = null;
List<String> identifiers = new Vector<String>();
{
String[] ids = request.getParameterValues("name");
if( null != ids ) {
for(String id : ids){
identifiers.add(id);
}
}
if( identifiers.size() > 0 ) {
identifier = identifiers.get(0);
}
if( null == identifier ) {
Exception e = new Exception("Unknown name");
reportError(response,HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e);
return;
}
logger.debug("Export Name: "+identifier);
}
// Parse contentType
String contentType = null;
{
String[] contentTypes = request.getParameterValues("contentType");
if( null != contentTypes ) {
for(String t : contentTypes){
contentType = t;
}
}
if( null != contentType ) {
logger.debug("Content-Type: "+contentType);
}
}
// Build doc retrieval based on method
DocumentRetrieval docRetrieval = null;
if( Method.LAYER == method ) {
try {
docRetrieval = DocumentRetrievalLayer.create(configuration.getCouchDb(), identifier);
} catch (Exception e) {
throw new ServletException("Problem retrieving documents from layer: "+identifier,e);
}
} else if( Method.SCHEMA == method ) {
try {
docRetrieval = DocumentRetrievalSchema.create(configuration.getCouchDb(), identifier);
} catch (Exception e) {
throw new ServletException("Problem retrieving documents from schema: "+identifier,e);
}
} else if( Method.DOC_ID == method ) {
try {
docRetrieval = DocumentRetrievalId.create(configuration.getCouchDb(), identifiers);
} catch (Exception e) {
throw new ServletException("Problem retrieving documents from doc ids: "+identifiers,e);
}
} else {
throw new ServletException("Do not know how to handle method: "+method.name());
}
// Build document filter based on filter type
DocumentFilter docFilter = null;
{
docFilter = new DocumentFilterGeometryType(filter);
}
ExportFormat outputFormat = null;
if( Format.GEOJSON == format ) {
try {
SchemaCache schemaCache = new SchemaCacheCouchDb(configuration.getCouchDb());
outputFormat = new ExportFormatGeoJson(schemaCache, docRetrieval, docFilter);
} catch (Exception e) {
throw new ServletException("Problem setting up format: "+format.name(),e);
}
} else {
throw new ServletException("Do not know how to handle format: "+format.name());
}
String charEncoding = outputFormat.getCharacterEncoding();
if( null != charEncoding ) {
response.setCharacterEncoding( charEncoding );