*/
private static final Log log = LogFactory.getLog(GadgetZipUploadHandler.class);
public void put(RequestContext requestContext) throws RegistryException {
Registry sysRegistry = GadgetRepoContext.getRegistryService().getConfigSystemRegistry();
// adding the stream from the resource to the zip input
String gadgetName = requestContext.getResource().getProperty(DashboardConstants.GADGET_NAME);
ZipInputStream zis = new ZipInputStream(requestContext.getResource().getContentStream());
ZipEntry zipentry = null;
int i;
byte[] buff = new byte[1024];
try {
zipentry = zis.getNextEntry();
if (zipentry == null) {
throw new RegistryException("Gadget bundle should be a zip file");
}
} catch (IOException e) {
log.error(e);
}
sysRegistry.beginTransaction();
while (zipentry != null) {
String entryName = zipentry.getName();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
if (zipentry.isDirectory()) {
//if the entry is a directory creating a collection
Collection col = sysRegistry.newCollection();
sysRegistry.put(DashboardConstants.GS_REGISTRY_ROOT +
DashboardConstants.GADGET_PATH + gadgetName
+ RegistryConstants.PATH_SEPARATOR + entryName, col);
} else {
//if a file, creating a resource
Resource res = sysRegistry.newResource();
try {
while ((i = zis.read(buff, 0, buff.length)) > 0) {
byteOut.write(buff, 0, i);
}
} catch (IOException ioe) {
sysRegistry.rollbackTransaction();
log.error(ioe);
}
res.setContent(byteOut.toByteArray());
// If entry is gadget xml then set its media type.
if (entryName.contains(".xml")) {
res.setMediaType("application/vnd.wso2-gadget+xml");
}
sysRegistry.put(DashboardConstants.GS_REGISTRY_ROOT +
DashboardConstants.GADGET_PATH + gadgetName +
RegistryConstants.PATH_SEPARATOR + entryName, res);
}
try {
zipentry = zis.getNextEntry();
} catch (IOException e) {
sysRegistry.rollbackTransaction();
log.error(e);
}
}
try {
zis.close();
} catch (IOException e) {
log.error(e);
}
sysRegistry.commitTransaction();
requestContext.setProcessingComplete(true);
}