Search in sources :

Example 1 with KapuaUnauthenticatedException

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);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Device(org.eclipse.kapua.service.device.registry.Device) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) SortOrder(org.eclipse.kapua.commons.model.query.FieldSortCriteria.SortOrder) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) ServletException(javax.servlet.ServletException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) IOException(java.io.IOException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) ServletException(javax.servlet.ServletException) FieldSortCriteria(org.eclipse.kapua.commons.model.query.FieldSortCriteria) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceQuery(org.eclipse.kapua.service.device.registry.DeviceQuery) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException)

Example 2 with KapuaUnauthenticatedException

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);
    }
}
Also used : DeviceCommandOutput(org.eclipse.kapua.service.device.management.command.DeviceCommandOutput) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DeviceCommandManagementService(org.eclipse.kapua.service.device.management.command.DeviceCommandManagementService) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) ServletException(javax.servlet.ServletException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) IOException(java.io.IOException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) FileUploadException(org.apache.commons.fileupload.FileUploadException) ServletException(javax.servlet.ServletException) FileItem(org.apache.commons.fileupload.FileItem) StringTokenizer(java.util.StringTokenizer) DeviceCommandInput(org.eclipse.kapua.service.device.management.command.DeviceCommandInput) DeviceCommandFactory(org.eclipse.kapua.service.device.management.command.DeviceCommandFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException)

Example 3 with KapuaUnauthenticatedException

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);
    }
}
Also used : ServletException(javax.servlet.ServletException) FileItem(org.apache.commons.fileupload.FileItem) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) ServletException(javax.servlet.ServletException) KapuaUnauthenticatedException(org.eclipse.kapua.KapuaUnauthenticatedException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) IOException(java.io.IOException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) FileUploadException(org.apache.commons.fileupload.FileUploadException) DeviceConfigurationManagementService(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService)

Aggregations

IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)3 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)3 KapuaUnauthenticatedException (org.eclipse.kapua.KapuaUnauthenticatedException)3 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)3 FileItem (org.apache.commons.fileupload.FileItem)2 FileUploadException (org.apache.commons.fileupload.FileUploadException)2 StringTokenizer (java.util.StringTokenizer)1 FieldSortCriteria (org.eclipse.kapua.commons.model.query.FieldSortCriteria)1 SortOrder (org.eclipse.kapua.commons.model.query.FieldSortCriteria.SortOrder)1 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)1 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)1 DeviceCommandFactory (org.eclipse.kapua.service.device.management.command.DeviceCommandFactory)1 DeviceCommandInput (org.eclipse.kapua.service.device.management.command.DeviceCommandInput)1 DeviceCommandManagementService (org.eclipse.kapua.service.device.management.command.DeviceCommandManagementService)1 DeviceCommandOutput (org.eclipse.kapua.service.device.management.command.DeviceCommandOutput)1 DeviceConfigurationManagementService (org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService)1 Device (org.eclipse.kapua.service.device.registry.Device)1 DeviceFactory (org.eclipse.kapua.service.device.registry.DeviceFactory)1