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
//
W3CWidgetFactory fac = W3CWidgetFactoryUtils.createW3CWidgetFactory(getServletContext(), properties, localizedMessages);
W3CWidget widgetModel = fac.parse(zipFile);
String widgetId = widgetModel.getIdentifier();
//
// If we have a generated ID and resourceId, use the resourceId to override the generated widget identifier. This is a fix for WOOKIE-383
//
if (widgetModel.getIdentifier().startsWith("http://incubator.apache.org/wookie/generated/") && resourceId != null){
widgetId = resourceId;
}
new WidgetJavascriptSyntaxAnalyzer(fac.getUnzippedWidgetDirectory());
//
// Check if the widget model corresponds to an existing installed widget
//
IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
if (persistenceManager.findWidgetByGuid(widgetId) == null) {
//
// A new widget was created, so return 201
//
WidgetFactory.addNewWidget(widgetModel, zipFile, true);
NewWidgetBroadcaster.broadcast(properties, widgetModel.getIdentifier());
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(widgetId), true, 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$
}
}