use of org.eclipse.kapua.KapuaUnauthenticatedException in project kapua by eclipse.
the class DeviceExporterServlet method internalDoGet.
private void internalDoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// parameter extraction
String format = request.getParameter("format");
String scopeIdString = request.getParameter("scopeIdString");
// data exporter
DeviceExporter deviceExporter = null;
if ("xls".equals(format)) {
deviceExporter = new DeviceExporterExcel(response);
} else if ("csv".equals(format)) {
deviceExporter = new DeviceExporterCsv(response);
} else {
throw new IllegalArgumentException("format");
}
if (scopeIdString == null || scopeIdString.isEmpty()) {
throw new IllegalArgumentException("account");
}
deviceExporter.init(scopeIdString);
//
// get the devices and append them to the exporter
KapuaLocator locator = KapuaLocator.getInstance();
DeviceRegistryService drs = locator.getService(DeviceRegistryService.class);
DeviceFactory drf = locator.getFactory(DeviceFactory.class);
int resultsCount = 0;
int offset = 0;
// paginate through the matching message
DeviceQuery dq = drf.newQuery(KapuaEid.parseShortId(scopeIdString));
dq.setLimit(250);
// Inserting filter parameter if specified
AndPredicate andPred = new AndPredicate();
String clientId = request.getParameter("clientId");
if (clientId != null && !clientId.isEmpty()) {
andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.CLIENT_ID, clientId, Operator.STARTS_WITH));
}
String displayName = request.getParameter("displayName");
if (displayName != null && !displayName.isEmpty()) {
andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.DISPLAY_NAME, displayName, Operator.STARTS_WITH));
}
String serialNumber = request.getParameter("serialNumber");
if (serialNumber != null && !serialNumber.isEmpty()) {
andPred = andPred.and(new AttributePredicate<String>(DevicePredicates.SERIAL_NUMBER, serialNumber));
}
String deviceStatus = request.getParameter("deviceStatus");
if (deviceStatus != null && !deviceStatus.isEmpty()) {
andPred = andPred.and(new AttributePredicate<DeviceStatus>(DevicePredicates.STATUS, DeviceStatus.valueOf(deviceStatus)));
}
String sortAttribute = request.getParameter("sortAttribute");
if (sortAttribute != null && !sortAttribute.isEmpty()) {
String sortOrderString = request.getParameter("sortOrder");
SortOrder sortOrder;
if (sortOrderString != null && !sortOrderString.isEmpty()) {
sortOrder = SortOrder.valueOf(sortOrderString);
} else {
sortOrder = SortOrder.ASCENDING;
}
if (sortAttribute.compareTo("CLIENT_ID") == 0) {
dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.CLIENT_ID, sortOrder));
} else if (sortAttribute.compareTo("DISPLAY_NAME") == 0) {
dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.DISPLAY_NAME, sortOrder));
} else if (sortAttribute.compareTo("LAST_EVENT_ON") == 0) {
dq.setSortCriteria(new FieldSortCriteria(DevicePredicates.LAST_EVENT_ON, sortOrder));
}
}
dq.setPredicate(andPred);
KapuaListResult<Device> results = null;
do {
dq.setOffset(offset);
results = drs.query(dq);
deviceExporter.append(results);
offset += results.getSize();
resultsCount += results.getSize();
} while (results.getSize() > 0);
// Close things up
deviceExporter.close();
} catch (IllegalArgumentException iae) {
response.sendError(400, "Illegal value for query parameter: " + iae.getMessage());
return;
} catch (KapuaEntityNotFoundException eenfe) {
response.sendError(400, eenfe.getMessage());
return;
} catch (KapuaUnauthenticatedException eiae) {
response.sendError(401, eiae.getMessage());
return;
} catch (KapuaIllegalAccessException eiae) {
response.sendError(403, eiae.getMessage());
return;
} catch (Exception e) {
s_logger.error("Error creating device export", e);
throw new ServletException(e);
}
}
use of org.eclipse.kapua.KapuaUnauthenticatedException in project kapua by eclipse.
the class UploadRequest method doPostCommand.
private void doPostCommand(KapuaFormFields kapuaFormFields, HttpServletResponse resp) throws ServletException, IOException {
try {
List<FileItem> fileItems = kapuaFormFields.getFileItems();
final String scopeIdString = kapuaFormFields.get("scopeIdString");
final String deviceIdString = kapuaFormFields.get("deviceIdString");
final String command = kapuaFormFields.get("command");
final String password = kapuaFormFields.get("password");
final String timeoutString = kapuaFormFields.get("timeout");
if (scopeIdString == null || scopeIdString.isEmpty()) {
throw new IllegalArgumentException("scopeId");
}
if (deviceIdString == null || deviceIdString.isEmpty()) {
throw new IllegalArgumentException("deviceId");
}
if (command == null || command.isEmpty()) {
throw new IllegalArgumentException("command");
}
if (fileItems.size() > 1) {
throw new IllegalArgumentException("file");
}
Integer timeout = null;
if (timeoutString != null && !timeoutString.isEmpty()) {
try {
timeout = Integer.parseInt(timeoutString);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("timeout");
}
}
KapuaLocator locator = KapuaLocator.getInstance();
DeviceCommandManagementService deviceService = locator.getService(DeviceCommandManagementService.class);
// FIXME: set a max size on the MQtt payload
byte[] data = fileItems.size() == 0 ? null : fileItems.get(0).get();
DeviceCommandFactory deviceCommandFactory = locator.getFactory(DeviceCommandFactory.class);
DeviceCommandInput commandInput = deviceCommandFactory.newCommandInput();
StringTokenizer st = new StringTokenizer(command);
int count = st.countTokens();
String cmd = count > 0 ? st.nextToken() : null;
String[] args = count > 1 ? new String[count - 1] : null;
int i = 0;
while (st.hasMoreTokens()) {
args[i++] = st.nextToken();
}
commandInput.setCommand(cmd);
commandInput.setPassword((password == null || password.isEmpty()) ? null : password);
commandInput.setArguments(args);
commandInput.setTimeout(timeout);
commandInput.setWorkingDir("/tmp/");
commandInput.setBody(data);
DeviceCommandOutput deviceCommandOutput = deviceService.exec(KapuaEid.parseShortId(scopeIdString), KapuaEid.parseShortId(deviceIdString), commandInput, null);
resp.setContentType("text/plain");
if (deviceCommandOutput.getStderr() != null && deviceCommandOutput.getStderr().length() > 0) {
resp.getWriter().println(deviceCommandOutput.getStderr());
}
if (deviceCommandOutput.getStdout() != null && deviceCommandOutput.getStdout().length() > 0) {
resp.getWriter().println(deviceCommandOutput.getStdout());
}
if (deviceCommandOutput.getExceptionMessage() != null && deviceCommandOutput.getExceptionMessage().length() > 0) {
resp.getWriter().println(deviceCommandOutput.getExceptionMessage());
}
} catch (IllegalArgumentException iae) {
resp.sendError(400, "Illegal value for query parameter: " + iae.getMessage());
return;
} catch (KapuaEntityNotFoundException eenfe) {
resp.sendError(400, eenfe.getMessage());
return;
} catch (KapuaUnauthenticatedException eiae) {
resp.sendError(401, eiae.getMessage());
return;
} catch (KapuaIllegalAccessException eiae) {
resp.sendError(403, eiae.getMessage());
return;
} catch (Exception e) {
s_logger.error("Error sending command", e);
throw new ServletException(e);
}
}
use of org.eclipse.kapua.KapuaUnauthenticatedException in project kapua by eclipse.
the class UploadRequest method doPostConfigurationSnapshot.
private void doPostConfigurationSnapshot(KapuaFormFields kapuaFormFields, HttpServletResponse resp) throws ServletException, IOException {
try {
List<FileItem> fileItems = kapuaFormFields.getFileItems();
String scopeIdString = kapuaFormFields.get("scopeIdString");
String deviceIdString = kapuaFormFields.get("deviceIdString");
if (scopeIdString == null || scopeIdString.isEmpty()) {
throw new IllegalArgumentException("scopeIdString");
}
if (deviceIdString == null || deviceIdString.isEmpty()) {
throw new IllegalArgumentException("deviceIdString");
}
if (fileItems == null || fileItems.size() != 1) {
throw new IllegalArgumentException("configuration");
}
KapuaLocator locator = KapuaLocator.getInstance();
DeviceConfigurationManagementService deviceConfigurationManagementService = locator.getService(DeviceConfigurationManagementService.class);
FileItem fileItem = fileItems.get(0);
byte[] data = fileItem.get();
String xmlConfigurationString = new String(data, "UTF-8");
deviceConfigurationManagementService.put(KapuaEid.parseShortId(scopeIdString), KapuaEid.parseShortId(deviceIdString), xmlConfigurationString, null);
} catch (IllegalArgumentException iae) {
resp.sendError(400, "Illegal value for query parameter: " + iae.getMessage());
return;
} catch (KapuaEntityNotFoundException eenfe) {
resp.sendError(400, eenfe.getMessage());
return;
} catch (KapuaUnauthenticatedException eiae) {
resp.sendError(401, eiae.getMessage());
return;
} catch (KapuaIllegalAccessException eiae) {
resp.sendError(403, eiae.getMessage());
return;
} catch (Exception e) {
s_logger.error("Error posting configuration", e);
throw new ServletException(e);
}
}
Aggregations