}
protected Representation processMultipartForm(PipelineConfiguration pipeconfig, Representation entity, Variant variant) {
String id = (String) getRequest().getAttributes().get("id");
XPipeline xpipeline = pipeconfig.pipeline;
XProcRuntime runtime = pipeconfig.runtime;
if (pipeconfig.ran) {
pipeconfig.reset();
xpipeline.reset();
}
String message = "";
HashMap<String,String> nameValuePairs = new HashMap<String,String> ();
HashMap<String,String> nsBindings = new HashMap<String,String> ();
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(100240);
RestletFileUpload upload = new RestletFileUpload(factory);
List<FileItem> items;
try {
items = upload.parseRequest(getRequest());
File file = null;
String filename = null;
for (final Iterator<FileItem> it = items.iterator(); it.hasNext(); ) {
FileItem fi = it.next();
String fieldName = fi.getFieldName();
String name = fi.getName();
if (name == null) {
Matcher matcher = xmlnsRE.matcher(fieldName);
if (matcher.matches()) {
nsBindings.put(matcher.group(1), new String(fi.get(), "utf-8"));
} else {
nameValuePairs.put(fieldName, new String(fi.get(), "utf-8"));
}
} else {
String port = fieldName;
if (pipeconfig.documentCount(port) == 0) {
xpipeline.clearInputs(port);
}
pipeconfig.writeTo(port);
try {
XdmNode doc = null;
MediaType m = new MediaType(fi.getContentType());
if (isXml(m)) {
doc = runtime.parse(new InputSource(fi.getInputStream()));
logger.debug("Posting XML document to " + port + " for " + id);
} else {
ReadablePipe pipe = null;
pipe = new ReadableData(runtime, XProcConstants.c_data, fi.getInputStream(), fi.getContentType());
doc = pipe.read();
logger.debug("Posting non-XML document to " + port + " for " + id);
}
xpipeline.writeTo(port, doc);
} catch (Exception e) {
throw new XProcException(e);
}
message += "Posted input to port '" + port + "'\n";
}
}
DeclareStep pipeline = xpipeline.getDeclareStep();
for (String fieldName : nameValuePairs.keySet()) {
RuntimeValue value = new RuntimeValue(nameValuePairs.get(fieldName));
if (fieldName.startsWith("-p")) {
fieldName = fieldName.substring(2);
String port= null;
Matcher matcher = portRE.matcher(fieldName);
if (matcher.matches()) {
port = matcher.group(1);
fieldName = matcher.group(2);
}
if (port == null) {
// Figure out the default parameter port
for (String iport : xpipeline.getInputs()) {
com.xmlcalabash.model.Input input = pipeline.getInput(iport);
if (input.getParameterInput() && input.getPrimary()) {
port = iport;
}
}
}
if (port == null) {
throw new XProcException("No primary parameter input port.");
}
logger.debug("Parameter " + fieldName + "=" + value.getString() + " for " + id);
QName qname = qnameFromForm(fieldName, nsBindings);
xpipeline.setParameter(port, qname, value);
pipeconfig.setParameter(qname, value.getString());
message += "Parameter " + qname.getClarkName() + "=" + value.getString() + "\n";
} else {
logger.debug("Option " + fieldName + "=" + value.getString() + " for " + id);
QName qname = qnameFromForm(fieldName, nsBindings);
xpipeline.passOption(qname, value);
pipeconfig.setGVOption(qname);
message += "Option " + qname.getClarkName() + "=" + value.getString() + "\n";
}
}
return okResponse(message, variant.getMediaType(), Status.SUCCESS_OK);
} catch (XProcException e) {
pipeconfig.reset();
xpipeline.reset();
throw e;
} catch (Exception e) {
pipeconfig.reset();
xpipeline.reset();
throw new XProcException(e);
}
}