use of org.eclipse.kura.web.server.util.ServiceLocator in project kura by eclipse.
the class DeviceSnapshotsServlet method doGet.
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
GwtXSRFToken token = new GwtXSRFToken(request.getParameter("xsrfToken"));
KuraRemoteServiceServlet.checkXSRFToken(request, token);
} catch (Exception e) {
throw new ServletException("Security error: please retry this operation correctly.", e);
}
// END XSRF security check
String snapshotId = request.getParameter("snapshotId");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/xml");
response.setHeader("Content-Disposition", "attachment; filename=snapshot_" + snapshotId + ".xml");
response.setHeader("Cache-Control", "no-transform, max-age=0");
PrintWriter writer = response.getWriter();
try {
ServiceLocator locator = ServiceLocator.getInstance();
ConfigurationService cs = locator.getService(ConfigurationService.class);
if (snapshotId != null) {
long sid = Long.parseLong(snapshotId);
List<ComponentConfiguration> configs = cs.getSnapshot(sid);
// build a list of configuration which can be marshalled in XML
List<ComponentConfigurationImpl> configImpls = new ArrayList<ComponentConfigurationImpl>();
for (ComponentConfiguration config : configs) {
configImpls.add((ComponentConfigurationImpl) config);
}
XmlComponentConfigurations xmlConfigs = new XmlComponentConfigurations();
xmlConfigs.setConfigurations(configImpls);
//
// marshall the response and write it
XmlUtil.marshal(xmlConfigs, writer);
}
} catch (Exception e) {
s_logger.error("Error creating Excel export", e);
throw new ServletException(e);
} finally {
if (writer != null) {
writer.close();
}
}
}
use of org.eclipse.kura.web.server.util.ServiceLocator in project kura by eclipse.
the class UploadRequest method getFileUploadInMemorySizeThreshold.
private static int getFileUploadInMemorySizeThreshold() {
ServiceLocator locator = ServiceLocator.getInstance();
int sizeThreshold = DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD;
try {
SystemService systemService = locator.getService(SystemService.class);
sizeThreshold = Integer.parseInt(systemService.getProperties().getProperty("file.upload.in.memory.size.threshold", String.valueOf(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD)));
} catch (GwtKuraException e) {
s_logger.error("Error locating SystemService", e);
}
return sizeThreshold;
}
use of org.eclipse.kura.web.server.util.ServiceLocator in project kura by eclipse.
the class UploadRequest method doPostDeploy.
private void doPostDeploy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServiceLocator locator = ServiceLocator.getInstance();
DeploymentAgentService deploymentAgentService;
try {
deploymentAgentService = locator.getService(DeploymentAgentService.class);
} catch (GwtKuraException e) {
s_logger.error("Error locating DeploymentAgentService", e);
throw new ServletException("Error locating DeploymentAgentService", e);
}
String reqPathInfo = req.getPathInfo();
if (reqPathInfo.endsWith("url")) {
String packageDownloadUrl = req.getParameter("packageUrl");
if (packageDownloadUrl == null) {
s_logger.error("Deployment package URL parameter missing");
throw new ServletException("Deployment package URL parameter missing");
}
// BEGIN XSRF - Servlet dependent code
String tokenId = req.getParameter("xsrfToken");
try {
GwtXSRFToken token = new GwtXSRFToken(tokenId);
KuraRemoteServiceServlet.checkXSRFToken(req, token);
} catch (Exception e) {
throw new ServletException("Security error: please retry this operation correctly.", e);
}
try {
s_logger.info("Installing package...");
deploymentAgentService.installDeploymentPackageAsync(packageDownloadUrl);
} catch (Exception e) {
s_logger.error("Failed to install package at URL {}", packageDownloadUrl, e);
throw new ServletException("Error installing deployment package", e);
}
} else if (reqPathInfo.endsWith("upload")) {
doPostDeployUpload(req, resp);
} else {
s_logger.error("Unsupported package deployment request");
throw new ServletException("Unsupported package deployment request");
}
}
use of org.eclipse.kura.web.server.util.ServiceLocator in project kura by eclipse.
the class UploadRequest method doGetIcon.
private void doGetIcon(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String queryString = req.getQueryString();
if (queryString == null) {
s_logger.error("Error parsing query string.");
throw new ServletException("Error parsing query string.");
}
// Parse the query string
Map<String, String> pairs;
try {
pairs = parseQueryString(queryString);
} catch (UnsupportedEncodingException e) {
s_logger.error("Error parsing query string.");
throw new ServletException("Error parsing query string: " + e.getLocalizedMessage());
}
// Check for malformed request
if (pairs == null || pairs.size() != 1) {
s_logger.error("Error parsing query string.");
throw new ServletException("Error parsing query string.");
}
String pid = pairs.get("pid");
if (pid != null && pid.length() > 0) {
BundleContext ctx = Console.getBundleContext();
Bundle[] bundles = ctx.getBundles();
ServiceLocator locator = ServiceLocator.getInstance();
// Iterate over bundles to find PID
for (Bundle b : bundles) {
MetaTypeService mts;
try {
mts = locator.getService(MetaTypeService.class);
} catch (GwtKuraException e1) {
s_logger.error("Error parsing query string.");
throw new ServletException("Error parsing query string.");
}
MetaTypeInformation mti = mts.getMetaTypeInformation(b);
String[] pids = mti.getPids();
for (String p : pids) {
if (p.equals(pid)) {
try {
InputStream is = mti.getObjectClassDefinition(pid, null).getIcon(32);
if (is == null) {
s_logger.error("Error reading icon file.");
throw new ServletException("Error reading icon file.");
}
OutputStream os = resp.getOutputStream();
byte[] buffer = new byte[1024];
for (int length = 0; (length = is.read(buffer)) > 0; ) {
os.write(buffer, 0, length);
}
is.close();
os.close();
} catch (IOException e) {
s_logger.error("Error reading icon file.");
throw new IOException("Error reading icon file.");
}
}
}
}
} else {
s_logger.error("Error parsing query string.");
throw new ServletException("Error parsing query string.");
}
}
use of org.eclipse.kura.web.server.util.ServiceLocator in project kura by eclipse.
the class UploadRequest method doPostConfigurationSnapshot.
private void doPostConfigurationSnapshot(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
UploadRequest upload = new UploadRequest(this.m_diskFileItemFactory);
try {
upload.parse(req);
} catch (FileUploadException e) {
s_logger.error("Error parsing the file upload request");
throw new ServletException("Error parsing the file upload request", e);
}
// BEGIN XSRF - Servlet dependent code
Map<String, String> formFields = upload.getFormFields();
try {
GwtXSRFToken token = new GwtXSRFToken(formFields.get("xsrfToken"));
KuraRemoteServiceServlet.checkXSRFToken(req, token);
} catch (Exception e) {
throw new ServletException("Security error: please retry this operation correctly.", e);
}
// END XSRF security check
List<FileItem> fileItems = upload.getFileItems();
if (fileItems.size() != 1) {
s_logger.error("expected 1 file item but found {}", fileItems.size());
throw new ServletException("Wrong number of file items");
}
FileItem fileItem = fileItems.get(0);
byte[] data = fileItem.get();
String xmlString = new String(data, "UTF-8");
XmlComponentConfigurations xmlConfigs;
try {
xmlConfigs = XmlUtil.unmarshal(xmlString, XmlComponentConfigurations.class);
} catch (Exception e) {
s_logger.error("Error unmarshaling device configuration", e);
throw new ServletException("Error unmarshaling device configuration", e);
}
ServiceLocator locator = ServiceLocator.getInstance();
try {
ConfigurationService cs = locator.getService(ConfigurationService.class);
List<ComponentConfigurationImpl> configImpls = xmlConfigs.getConfigurations();
List<ComponentConfiguration> configs = new ArrayList<ComponentConfiguration>();
configs.addAll(configImpls);
cs.updateConfigurations(configs);
//
// Add an additional delay after the configuration update
// to give the time to the device to apply the received
// configuration
SystemService ss = locator.getService(SystemService.class);
long delay = Long.parseLong(ss.getProperties().getProperty("console.updateConfigDelay", "5000"));
if (delay > 0) {
Thread.sleep(delay);
}
} catch (Exception e) {
s_logger.error("Error updating device configuration: {}", e);
throw new ServletException("Error updating device configuration", e);
}
}
Aggregations