final String UPLOADFOLDER = getServletContext().getRealPath(properties.getString("widget.useruploadfolder"));//$NON-NLS-1$
//
// Get localized messages so we can return errors
//
Messages localizedMessages = LocaleHandler.localizeMessages(request);
//
// Try to obtain a zipfile from the request.
//
File zipFile;
try {
zipFile = WidgetFileUtils.upload(UPLOADFOLDER, request);
} catch (Exception ex) {
throw new InvalidParametersException(localizedMessages.getString("widgets.invalid-config-xml") + "\n" + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
}
//
// No file uploaded
//
if(zipFile == null || !zipFile.exists()){
throw new InvalidParametersException(localizedMessages.getString("widgets.no-widget-file-uploaded")); //$NON-NLS-1$
}
try {
//
// Parse and validate the zip as a widget
//
final String[] locales = properties.getStringArray("widget.locales");
W3CWidgetFactory fac = new W3CWidgetFactory();
fac.setLocales(locales);
fac.setLocalPath(getServletContext().getContextPath() + properties.getString("widget.widgetfolder"));
fac.setOutputDirectory(WIDGETFOLDER);
fac.setFeatures(Features.getFeatureNames());
fac.setStartPageProcessor(new StartPageProcessor());
W3CWidget widgetModel = fac.parse(zipFile);
new WidgetJavascriptSyntaxAnalyzer(fac.getUnzippedWidgetDirectory());
// File f = new File();
//
// Check if the widget model corresponds to an existing installed widget
//
IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
if (persistenceManager.findWidgetByGuid(widgetModel.getIdentifier()) == null) {
//
// A new widget was created, so return 201
//
WidgetFactory.addNewWidget(widgetModel, zipFile,false);
returnXml(WidgetImportHelper.createXMLWidgetDocument(widgetModel, new File(fac.getUnzippedWidgetDirectory(), "config.xml"), getWookieServerURL(request, "").toString(), true), response);
return true;
} else {
//
// Widget already exists, so update the widget metadata and configuration details
// and return 200
//
WidgetFactory.update(widgetModel,persistenceManager.findWidgetByGuid(widgetModel.getIdentifier()),false, zipFile);
returnXml(WidgetImportHelper.createXMLWidgetDocument(widgetModel, new File(fac.getUnzippedWidgetDirectory(), "config.xml"), getWookieServerURL(request, "").toString(), true), response);
return false;
}
//
// Catch specific parsing and validation errors and throw exception with error message
//
} catch (InvalidStartFileException ex) {
_logger.error(ex);
throw new InvalidParametersException(
localizedMessages.getString("widgets.no-start-file") + "\n" + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
} catch (BadManifestException ex) {
_logger.error(ex);
String message = ex.getMessage();
if (ex.getMessage() == null || ex.getMessage().equals(""))message = localizedMessages.getString("widgets.invalid-config-xml"); //$NON-NLS-1$
if (ex instanceof InvalidContentTypeException)
message = localizedMessages.getString("widgets.unsupported-content-type");//$NON-NLS-1$
throw new InvalidParametersException(message);
} catch (BadWidgetZipFileException ex) {
_logger.error(ex);
String message = ex.getMessage();
if (ex.getMessage() == null || ex.getMessage().equals(""))message = localizedMessages.getString("widgets.bad-zip-file"); //$NON-NLS-1$
throw new InvalidParametersException(message);
} catch (Exception ex) {
_logger.error(ex);
throw new InvalidParametersException(
localizedMessages.getString("widgets.cant-parse-config-xml") + "\n" + ex.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
}