private void verifyFile(ActionEvent event, RequestContext context)
throws Exception {
// extract the uploaded file information from the request,
// ensure that a file with valid XML content was supplied
MessageBroker msgBroker = extractMessageBroker();
String sFileName = "";
try {
FileItem item = extractFileItem();
if (item != null) {
sFileName = Val.chkStr(item.getName());
String sXml = Val.chkStr(item.getString("UTF-8"));
// remove the UTF-8 byte order mark if present
byte[] bom = new byte[3];
bom[0] = (byte)0xEF;
bom[1] = (byte)0xBB;
bom[2] = (byte)0xBF;
String sbom = new String(bom,"UTF-8");
if (sXml.startsWith(sbom)) {
sXml = Val.chkStr(sXml.substring(1));
}
if (sFileName.length() == 0) {
msgBroker.addErrorMessage("publication.validateMetadata.err.file.required");
} else if (sXml.length() == 0) {
msgBroker.addErrorMessage("publication.validateMetadata.err.file.empty");
} else {
//String sOut = "C:/xfer/test19139.xml";
//com.esri.gpt.framework.xml.XmlIoUtil.writeXmlFile(sXml,new java.io.File(sOut));
// verifies the file, set the success message
ValidationRequest request = new ValidationRequest(context,sFileName,sXml);
request.verify();
msgBroker.addSuccessMessage("catalog.publication.success.validated");
}
}
} catch (ValidationException e) {
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_WARN,sFileName,null);
msgBroker.addMessage(fm);
String sKey = e.getKey();
if (sKey.length() > 0) {
String sMsg = sKey;
Schema schema = context.getCatalogConfiguration().getConfiguredSchemas().get(sKey);
if (schema != null) {
if (schema.getLabel() != null) {
String sResKey = schema.getLabel().getResourceKey();
if (sResKey.length() > 0) {
sMsg = extractMessageBroker().retrieveMessage(sResKey)+" ("+sKey+")";
}
}
}
fm = new FacesMessage(FacesMessage.SEVERITY_WARN," - "+sMsg,null);
extractMessageBroker().addMessage(fm);
}
e.getValidationErrors().buildMessages(msgBroker,true);
} catch (Exception e) {
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_WARN,sFileName,null);
msgBroker.addMessage(fm);
// there seems to be no good exception related to a file that is simply
// not an XML file, a message containing "content is not allowed in prolog"
// seems to be the best guess at the moment
String sMsg = e.toString().toLowerCase();
if (sMsg.indexOf("content is not allowed in prolog") != -1) {
msgBroker.addErrorMessage("publication.validateMetadata.err.file.prolog");
} else {
throw e;
}
}
}