Example usage for javax.servlet.http HttpServletResponse SC_EXPECTATION_FAILED

List of usage examples for javax.servlet.http HttpServletResponse SC_EXPECTATION_FAILED

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_EXPECTATION_FAILED.

Prototype

int SC_EXPECTATION_FAILED

To view the source code for javax.servlet.http HttpServletResponse SC_EXPECTATION_FAILED.

Click Source Link

Document

Status code (417) indicating that the server could not meet the expectation given in the Expect request header.

Usage

From source file:org.wso2.carbon.analytics.servlet.AnalyticsRecordProcessor.java

/**
 * Put records//from   w  w w  .j  a  v a  2  s  . com
 *
 * @param req  HttpRequest which has the required parameters to do the operation.
 * @param resp HttpResponse which returns the result of the intended operation.
 * @throws ServletException
 * @throws IOException
 */
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String sessionId = req.getHeader(AnalyticsAPIConstants.SESSION_ID);
    if (sessionId == null || sessionId.trim().isEmpty()) {
        resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
    } else {
        try {
            ServiceHolder.getAuthenticator().validateSessionId(sessionId);
        } catch (AnalyticsAPIAuthenticationException e) {
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
        }
        String operation = req.getParameter(AnalyticsAPIConstants.OPERATION);
        boolean securityEnabled = Boolean
                .parseBoolean(req.getParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM));
        if (operation != null
                && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.PUT_RECORD_OPERATION)) {
            String username = req.getParameter(AnalyticsAPIConstants.USERNAME_PARAM);
            try {
                List<Record> records = (List<Record>) GenericUtils.deserializeObject(req.getInputStream());
                if (!securityEnabled)
                    ServiceHolder.getAnalyticsDataService().put(records);
                else
                    ServiceHolder.getSecureAnalyticsDataService().put(username, records);
                List<String> recordIds = new ArrayList<>();
                for (Record record : records) {
                    recordIds.add(record.getId());
                }
                resp.getOutputStream().write(GenericUtils.serializeObject(recordIds));
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE,
                    "unsupported operation performed with get request!");
            log.error("unsupported operation performed : " + operation + " with get request!");
        }
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsRecordProcessor.java

/**
 * delete records for range and given ids..
 *
 * @param req  HttpRequest which has the required parameters to do the operation.
 * @param resp HttpResponse which returns the result of the intended operation.
 * @throws ServletException/*from w  w  w. j  ava2  s  . c o m*/
 * @throws IOException
 */
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String sessionId = req.getHeader(AnalyticsAPIConstants.SESSION_ID);
    if (sessionId == null || sessionId.trim().isEmpty()) {
        resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
    } else {
        try {
            ServiceHolder.getAuthenticator().validateSessionId(sessionId);
        } catch (AnalyticsAPIAuthenticationException e) {
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
        }
        String operation = req.getParameter(AnalyticsAPIConstants.OPERATION);
        boolean securityEnabled = Boolean
                .parseBoolean(req.getParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM));
        int tenantIdParam = MultitenantConstants.INVALID_TENANT_ID;
        if (!securityEnabled)
            tenantIdParam = Integer.parseInt(req.getParameter(AnalyticsAPIConstants.TENANT_ID_PARAM));
        String userName = req.getParameter(AnalyticsAPIConstants.USERNAME_PARAM);
        String tableName = req.getParameter(AnalyticsAPIConstants.TABLE_NAME_PARAM);
        if (operation != null
                && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.DELETE_RECORDS_RANGE_OPERATION)) {
            long timeFrom = Long.parseLong(req.getParameter(AnalyticsAPIConstants.TIME_FROM_PARAM));
            long timeTo = Long.parseLong(req.getParameter(AnalyticsAPIConstants.TIME_TO_PARAM));
            try {
                if (!securityEnabled)
                    ServiceHolder.getAnalyticsDataService().delete(tenantIdParam, tableName, timeFrom, timeTo);
                else
                    ServiceHolder.getSecureAnalyticsDataService().delete(userName, tableName, timeFrom, timeTo);
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else if (operation != null
                && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.DELETE_RECORDS_IDS_OPERATION)) {
            String jsonRecordIds = req.getParameter(AnalyticsAPIConstants.RECORD_IDS_PARAM);
            Type recordIdListType = new TypeToken<List<String>>() {
            }.getType();
            List<String> recordIds = new Gson().fromJson(jsonRecordIds, recordIdListType);
            try {
                if (!securityEnabled)
                    ServiceHolder.getAnalyticsDataService().delete(tenantIdParam, tableName, recordIds);
                else
                    ServiceHolder.getSecureAnalyticsDataService().delete(userName, tableName, recordIds);
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE,
                    "unsupported operation performed with get request!");
            log.error("unsupported operation performed : " + operation + " with post request!");
        }
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsSearchProcessor.java

/**
 * Search the table/*from  w ww  .j  a  v  a 2  s.c o  m*/
 *
 * @param req  HttpRequest which has the required parameters to do the operation.
 * @param resp HttpResponse which returns the result of the intended operation.
 * @throws ServletException
 * @throws IOException
 */
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String sessionId = req.getHeader(AnalyticsAPIConstants.SESSION_ID);
    if (sessionId == null || sessionId.trim().isEmpty()) {
        resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
    } else {
        try {
            ServiceHolder.getAuthenticator().validateSessionId(sessionId);
        } catch (AnalyticsAPIAuthenticationException e) {
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
        }
        String operation = req.getParameter(AnalyticsAPIConstants.OPERATION);
        boolean securityEnabled = Boolean
                .parseBoolean(req.getParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM));
        int tenantIdParam = MultitenantConstants.INVALID_TENANT_ID;
        if (!securityEnabled)
            tenantIdParam = Integer.parseInt(req.getParameter(AnalyticsAPIConstants.TENANT_ID_PARAM));
        String userName = req.getParameter(AnalyticsAPIConstants.USERNAME_PARAM);
        String tableName = req.getParameter(AnalyticsAPIConstants.TABLE_NAME_PARAM);
        String query = req.getParameter(AnalyticsAPIConstants.QUERY);
        if (operation != null && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.SEARCH_OPERATION)) {
            int start = Integer.parseInt(req.getParameter(AnalyticsAPIConstants.START_PARAM));
            int count = Integer.parseInt(req.getParameter(AnalyticsAPIConstants.COUNT_PARAM));
            Type sortByFieldType = new TypeToken<List<SortByField>>() {
            }.getType();
            Gson gson = new Gson();
            List<SortByField> sortByFields = gson
                    .fromJson(req.getParameter(AnalyticsAPIConstants.SORT_BY_FIELDS_PARAM), sortByFieldType);
            try {
                List<SearchResultEntry> searchResult;
                if (!securityEnabled)
                    searchResult = ServiceHolder.getAnalyticsDataService().search(tenantIdParam, tableName,
                            query, start, count, sortByFields);
                else
                    searchResult = ServiceHolder.getSecureAnalyticsDataService().search(userName, tableName,
                            query, start, count, sortByFields);
                //Have to do this because there is possibility of getting sublist which cannot be serialized
                searchResult = new ArrayList<>(searchResult);
                resp.getOutputStream().write(GenericUtils.serializeObject(searchResult));
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else if (operation != null
                && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.SEARCH_COUNT_OPERATION)) {
            try {
                int count;
                if (!securityEnabled)
                    count = ServiceHolder.getAnalyticsDataService().searchCount(tenantIdParam, tableName,
                            query);
                else
                    count = ServiceHolder.getSecureAnalyticsDataService().searchCount(userName, tableName,
                            query);
                PrintWriter output = resp.getWriter();
                output.append(AnalyticsAPIConstants.SEARCH_COUNT).append(AnalyticsAPIConstants.SEPARATOR)
                        .append(String.valueOf(count));
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else if (operation != null
                && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.SEARCH_WITH_AGGREGATES_OPERATION)) {
            try {
                Gson gson = new Gson();
                AnalyticsIterator<Record> iterator;
                String groupByField = req.getParameter(AnalyticsAPIConstants.GROUP_BY_FIELD_PARAM);
                Type aggregateFieldsMapType = new TypeToken<List<AggregateField>>() {
                }.getType();
                List<AggregateField> fields = gson.fromJson(
                        req.getParameter(AnalyticsAPIConstants.AGGREGATING_FIELDS), aggregateFieldsMapType);
                String parentPathAsString = req.getParameter(AnalyticsAPIConstants.AGGREGATE_PARENT_PATH);
                Type aggregateParentPath = new TypeToken<List<String>>() {
                }.getType();
                List<String> parentPath = gson.fromJson(parentPathAsString, aggregateParentPath);
                int aggregateLevel = Integer.parseInt(req.getParameter(AnalyticsAPIConstants.AGGREGATE_LEVEL));
                int noOfRecords = Integer
                        .parseInt(req.getParameter(AnalyticsAPIConstants.AGGREGATE_NO_OF_RECORDS));
                AggregateRequest aggregateRequest = new AggregateRequest();
                aggregateRequest.setTableName(tableName);
                aggregateRequest.setQuery(query);
                aggregateRequest.setFields(fields);
                aggregateRequest.setGroupByField(groupByField);
                aggregateRequest.setAggregateLevel(aggregateLevel);
                aggregateRequest.setParentPath(parentPath);
                aggregateRequest.setNoOfRecords(noOfRecords);
                if (!securityEnabled)
                    iterator = ServiceHolder.getAnalyticsDataService().searchWithAggregates(tenantIdParam,
                            aggregateRequest);
                else
                    iterator = ServiceHolder.getSecureAnalyticsDataService().searchWithAggregates(userName,
                            aggregateRequest);
                while (iterator.hasNext()) {
                    Record record = iterator.next();
                    GenericUtils.serializeObject(record, resp.getOutputStream());
                }
                iterator.close();
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE,
                    "unsupported operation performed with get request!");
            log.error("unsupported operation performed : " + operation + " with get request!");
        }
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsSearchProcessor.java

private void doDrillDownSearch(HttpServletRequest req, HttpServletResponse resp, boolean securityEnabled,
        int tenantIdParam, String userName) throws IOException {
    ServletInputStream servletInputStream = req.getInputStream();
    try {//from  w w w .j  av  a2s  . co  m
        AnalyticsDrillDownRequest drillDownRequest = (AnalyticsDrillDownRequest) GenericUtils
                .deserializeObject(servletInputStream);
        List<SearchResultEntry> drillDownResult = new ArrayList<>();
        if (!securityEnabled) {
            drillDownResult.addAll(
                    ServiceHolder.getAnalyticsDataService().drillDownSearch(tenantIdParam, drillDownRequest));
        } else {
            drillDownResult.addAll(
                    ServiceHolder.getSecureAnalyticsDataService().drillDownSearch(userName, drillDownRequest));
        }
        GenericUtils.serializeObject(drillDownResult, resp.getOutputStream());
        resp.setStatus(HttpServletResponse.SC_OK);
    } catch (AnalyticsException e) {
        resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsSearchProcessor.java

private void doDrillDownSearchCount(HttpServletRequest req, HttpServletResponse resp, boolean securityEnabled,
        int tenantIdParam, String userName) throws IOException {
    ServletInputStream servletInputStream = req.getInputStream();
    try {//www  .j  av a  2 s.c o  m
        AnalyticsDrillDownRequest drillDownRequest = (AnalyticsDrillDownRequest) GenericUtils
                .deserializeObject(servletInputStream);
        double resultCount;
        if (!securityEnabled) {
            resultCount = ServiceHolder.getAnalyticsDataService().drillDownSearchCount(tenantIdParam,
                    drillDownRequest);
        } else {
            resultCount = ServiceHolder.getSecureAnalyticsDataService().drillDownSearchCount(userName,
                    drillDownRequest);
        }
        GenericUtils.serializeObject(resultCount, resp.getOutputStream());
        resp.setStatus(HttpServletResponse.SC_OK);
    } catch (AnalyticsException e) {
        resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsSearchProcessor.java

private void doDrillDownCategories(HttpServletRequest req, HttpServletResponse resp, boolean securityEnabled,
        int tenantIdParam, String userName) throws IOException {
    ServletInputStream servletInputStream = req.getInputStream();
    try {/* ww w .  ja  v  a  2s .  c om*/
        CategoryDrillDownRequest drillDownRequest = (CategoryDrillDownRequest) GenericUtils
                .deserializeObject(servletInputStream);
        SubCategories subCategories;
        if (!securityEnabled) {
            subCategories = ServiceHolder.getAnalyticsDataService().drillDownCategories(tenantIdParam,
                    drillDownRequest);
        } else {
            subCategories = ServiceHolder.getSecureAnalyticsDataService().drillDownCategories(userName,
                    drillDownRequest);
        }
        GenericUtils.serializeObject(subCategories, resp.getOutputStream());
        resp.setStatus(HttpServletResponse.SC_OK);
    } catch (AnalyticsException e) {
        resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsSearchProcessor.java

private void doDrillDownRangeCount(HttpServletRequest req, HttpServletResponse resp, boolean securityEnabled,
        int tenantIdParam, String userName) throws IOException {
    ServletInputStream servletInputStream = req.getInputStream();
    try {//from w ww.ja  v a2s  . co m
        AnalyticsDrillDownRequest drillDownRequest = (AnalyticsDrillDownRequest) GenericUtils
                .deserializeObject(servletInputStream);
        List<AnalyticsDrillDownRange> ranges;
        if (!securityEnabled) {
            ranges = ServiceHolder.getAnalyticsDataService().drillDownRangeCount(tenantIdParam,
                    drillDownRequest);
        } else {
            ranges = ServiceHolder.getSecureAnalyticsDataService().drillDownRangeCount(userName,
                    drillDownRequest);
        }
        GenericUtils.serializeObject(ranges, resp.getOutputStream());
        resp.setStatus(HttpServletResponse.SC_OK);
    } catch (AnalyticsException e) {
        resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsSearchProcessor.java

@SuppressWarnings("unchecked")
private void doSearchMultiTablesWithAggregates(HttpServletRequest req, HttpServletResponse resp,
        boolean securityEnabled, int tenantIdParam, String userName) throws IOException {
    ServletInputStream servletInputStream = req.getInputStream();
    try {/*ww  w. j  av a  2  s . co m*/
        AggregateRequest[] requests = (AggregateRequest[]) GenericUtils.deserializeObject(servletInputStream);
        List<AnalyticsIterator<Record>> iterators;
        if (!securityEnabled) {
            iterators = ServiceHolder.getAnalyticsDataService().searchWithAggregates(tenantIdParam, requests);
        } else {
            iterators = ServiceHolder.getSecureAnalyticsDataService().searchWithAggregates(userName, requests);
        }
        List<List<Record>> aggregatedRecords = new ArrayList<>();
        for (AnalyticsIterator<Record> iterator : iterators) {
            aggregatedRecords.add(IteratorUtils.toList(iterator));
        }
        GenericUtils.serializeObject(aggregatedRecords, resp.getOutputStream());
        resp.setStatus(HttpServletResponse.SC_OK);
    } catch (AnalyticsException e) {
        resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsServiceProcessor.java

/**
 * Destroy the analytics service.//from  w w  w . java 2 s  . c o m
 *
 * @param req HttpRequest which has the required parameters to do the operation.
 * @param resp HttpResponse which returns the result of the intended operation.
 * @throws ServletException
 * @throws IOException
 */

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String sessionId = req.getHeader(AnalyticsAPIConstants.SESSION_ID);
    if (sessionId == null || sessionId.trim().isEmpty()) {
        resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
    } else {
        try {
            ServiceHolder.getAuthenticator().validateSessionId(sessionId);
        } catch (AnalyticsAPIAuthenticationException e) {
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
        }
        String operation = req.getParameter(AnalyticsAPIConstants.OPERATION);
        if (operation != null && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.DESTROY_OPERATION)) {
            try {
                ServiceHolder.getAnalyticsDataService().destroy();
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE,
                    "unsupported operation performed with post request!");
            log.error("unsupported operation performed : " + operation + " with post request!");
        }
    }
}

From source file:org.wso2.carbon.analytics.servlet.AnalyticsTableProcessor.java

/**
 * Get the all tables for tenant or check table exists
 *
 * @param req HttpRequest which has the required parameters to do the operation.
 * @param resp HttpResponse which returns the result of the intended operation.
 * @throws ServletException//from w  w  w  .j a va 2s. c om
 * @throws IOException
 */
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String sessionId = req.getHeader(AnalyticsAPIConstants.SESSION_ID);
    if (sessionId == null || sessionId.trim().isEmpty()) {
        resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
    } else {
        try {
            ServiceHolder.getAuthenticator().validateSessionId(sessionId);
        } catch (AnalyticsAPIAuthenticationException e) {
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "No session id found, Please login first!");
        }
        String operation = req.getParameter(AnalyticsAPIConstants.OPERATION);
        boolean securityEnabled = Boolean
                .parseBoolean(req.getParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM));
        int tenantIdParam = MultitenantConstants.INVALID_TENANT_ID;
        if (!securityEnabled)
            tenantIdParam = Integer.parseInt(req.getParameter(AnalyticsAPIConstants.TENANT_ID_PARAM));
        String userName = req.getParameter(AnalyticsAPIConstants.USERNAME_PARAM);
        if (operation != null
                && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.TABLE_EXISTS_OPERATION)) {
            String tableName = req.getParameter(AnalyticsAPIConstants.TABLE_NAME_PARAM);
            try {
                boolean tableExists;
                if (!securityEnabled)
                    tableExists = ServiceHolder.getAnalyticsDataService().tableExists(tenantIdParam, tableName);
                else
                    tableExists = ServiceHolder.getSecureAnalyticsDataService().tableExists(userName,
                            tableName);
                PrintWriter output = resp.getWriter();
                output.append(AnalyticsAPIConstants.TABLE_EXISTS).append(AnalyticsAPIConstants.SEPARATOR)
                        .append(String.valueOf(tableExists));
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else if (operation != null
                && operation.trim().equalsIgnoreCase(AnalyticsAPIConstants.LIST_TABLES_OPERATION)) {
            try {
                List<String> tableNames;
                if (!securityEnabled)
                    tableNames = ServiceHolder.getAnalyticsDataService().listTables(tenantIdParam);
                else
                    tableNames = ServiceHolder.getSecureAnalyticsDataService().listTables(userName);
                resp.getOutputStream().write(GenericUtils.serializeObject(tableNames));
                resp.setStatus(HttpServletResponse.SC_OK);
            } catch (AnalyticsException e) {
                resp.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e.getMessage());
            }
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE,
                    "unsupported operation performed with get request!");
            log.error("unsupported operation performed : " + operation + " with get request!");
        }
    }
}