Search in sources :

Example 11 with ResponseBuilder

use of org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder in project hbase by apache.

the class StorageClusterVersionResource method get.

@GET
@Produces({ MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON })
public Response get(@Context final UriInfo uriInfo) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("GET " + uriInfo.getAbsolutePath());
    }
    servlet.getMetrics().incrementRequests(1);
    try {
        StorageClusterVersionModel model = new StorageClusterVersionModel();
        model.setVersion(servlet.getAdmin().getClusterMetrics(EnumSet.of(Option.HBASE_VERSION)).getHBaseVersion());
        ResponseBuilder response = Response.ok(model);
        response.cacheControl(cacheControl);
        servlet.getMetrics().incrementSucessfulGetRequests(1);
        return response.build();
    } catch (IOException e) {
        servlet.getMetrics().incrementFailedGetRequests(1);
        return Response.status(Response.Status.SERVICE_UNAVAILABLE).type(MIMETYPE_TEXT).entity("Unavailable" + CRLF).build();
    }
}
Also used : IOException(java.io.IOException) ResponseBuilder(org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder) StorageClusterVersionModel(org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel) Produces(org.apache.hbase.thirdparty.javax.ws.rs.Produces) GET(org.apache.hbase.thirdparty.javax.ws.rs.GET)

Example 12 with ResponseBuilder

use of org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder in project hbase by apache.

the class RowResource method checkAndPut.

/**
 * Validates the input request parameters, parses columns from CellSetModel,
 * and invokes checkAndPut on HTable.
 *
 * @param model instance of CellSetModel
 * @return Response 200 OK, 304 Not modified, 400 Bad request
 */
Response checkAndPut(final CellSetModel model) {
    Table table = null;
    try {
        table = servlet.getTable(tableResource.getName());
        if (model.getRows().size() != 1) {
            servlet.getMetrics().incrementFailedPutRequests(1);
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Number of rows specified is not 1." + CRLF).build();
        }
        RowModel rowModel = model.getRows().get(0);
        byte[] key = rowModel.getKey();
        if (key == null) {
            key = rowspec.getRow();
        }
        List<CellModel> cellModels = rowModel.getCells();
        int cellModelCount = cellModels.size();
        if (key == null || cellModelCount <= 1) {
            servlet.getMetrics().incrementFailedPutRequests(1);
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Either row key is null or no data found for columns specified." + CRLF).build();
        }
        Put put = new Put(key);
        boolean retValue;
        CellModel valueToCheckCell = cellModels.get(cellModelCount - 1);
        byte[] valueToCheckColumn = valueToCheckCell.getColumn();
        byte[][] valueToPutParts = CellUtil.parseColumn(valueToCheckColumn);
        if (valueToPutParts.length == 2 && valueToPutParts[1].length > 0) {
            CellModel valueToPutCell = null;
            // and track if the check cell's latest value is also sent
            for (int i = 0, n = cellModelCount - 1; i < n; i++) {
                CellModel cell = cellModels.get(i);
                byte[] col = cell.getColumn();
                if (col == null) {
                    servlet.getMetrics().incrementFailedPutRequests(1);
                    return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column found to be null." + CRLF).build();
                }
                byte[][] parts = CellUtil.parseColumn(col);
                if (parts.length != 2) {
                    return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request" + CRLF).build();
                }
                put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(put.getRow()).setFamily(parts[0]).setQualifier(parts[1]).setTimestamp(cell.getTimestamp()).setType(Type.Put).setValue(cell.getValue()).build());
                if (Bytes.equals(col, valueToCheckCell.getColumn())) {
                    valueToPutCell = cell;
                }
            }
            if (valueToPutCell == null) {
                servlet.getMetrics().incrementFailedPutRequests(1);
                return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: The column to put and check do not match." + CRLF).build();
            } else {
                retValue = table.checkAndMutate(key, valueToPutParts[0]).qualifier(valueToPutParts[1]).ifEquals(valueToCheckCell.getValue()).thenPut(put);
            }
        } else {
            servlet.getMetrics().incrementFailedPutRequests(1);
            return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column incorrectly specified." + CRLF).build();
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("CHECK-AND-PUT " + put.toString() + ", returns " + retValue);
        }
        if (!retValue) {
            servlet.getMetrics().incrementFailedPutRequests(1);
            return Response.status(Response.Status.NOT_MODIFIED).type(MIMETYPE_TEXT).entity("Value not Modified" + CRLF).build();
        }
        ResponseBuilder response = Response.ok();
        servlet.getMetrics().incrementSucessfulPutRequests(1);
        return response.build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedPutRequests(1);
        return processException(e);
    } finally {
        if (table != null)
            try {
                table.close();
            } catch (IOException ioe) {
                LOG.debug("Exception received while closing the table", ioe);
            }
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) IOException(java.io.IOException) ResponseBuilder(org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder) Put(org.apache.hadoop.hbase.client.Put) IOException(java.io.IOException)

Example 13 with ResponseBuilder

use of org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder in project hbase by apache.

the class RowResource method getBinary.

@GET
@Produces(MIMETYPE_BINARY)
public Response getBinary(@Context final UriInfo uriInfo) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("GET " + uriInfo.getAbsolutePath() + " as " + MIMETYPE_BINARY);
    }
    servlet.getMetrics().incrementRequests(1);
    // return a single cell
    if (!rowspec.hasColumns() || rowspec.getColumns().length > 1) {
        servlet.getMetrics().incrementFailedGetRequests(1);
        return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Default 'GET' method only works if there is exactly 1 column " + "in the row. Using the 'Accept' header with one of these formats lets you " + "retrieve the entire row if it has multiple columns: " + // Same as the @Produces list for the get method.
        MIMETYPE_XML + ", " + MIMETYPE_JSON + ", " + MIMETYPE_PROTOBUF + ", " + MIMETYPE_PROTOBUF_IETF + CRLF).build();
    }
    MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
    try {
        ResultGenerator generator = ResultGenerator.fromRowSpec(tableResource.getName(), rowspec, null, !params.containsKey(NOCACHE_PARAM_NAME));
        if (!generator.hasNext()) {
            servlet.getMetrics().incrementFailedGetRequests(1);
            return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT).entity("Not found" + CRLF).build();
        }
        Cell value = generator.next();
        ResponseBuilder response = Response.ok(CellUtil.cloneValue(value));
        response.header("X-Timestamp", value.getTimestamp());
        servlet.getMetrics().incrementSucessfulGetRequests(1);
        return response.build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedGetRequests(1);
        return processException(e);
    }
}
Also used : ResponseBuilder(org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder) Cell(org.apache.hadoop.hbase.Cell) IOException(java.io.IOException) Produces(org.apache.hbase.thirdparty.javax.ws.rs.Produces) GET(org.apache.hbase.thirdparty.javax.ws.rs.GET)

Example 14 with ResponseBuilder

use of org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder in project hbase by apache.

the class RowResource method update.

Response update(final CellSetModel model, final boolean replace) {
    servlet.getMetrics().incrementRequests(1);
    if (servlet.isReadOnly()) {
        servlet.getMetrics().incrementFailedPutRequests(1);
        return Response.status(Response.Status.FORBIDDEN).type(MIMETYPE_TEXT).entity("Forbidden" + CRLF).build();
    }
    if (CHECK_PUT.equalsIgnoreCase(check)) {
        return checkAndPut(model);
    } else if (CHECK_DELETE.equalsIgnoreCase(check)) {
        return checkAndDelete(model);
    } else if (CHECK_APPEND.equalsIgnoreCase(check)) {
        return append(model);
    } else if (CHECK_INCREMENT.equalsIgnoreCase(check)) {
        return increment(model);
    } else if (check != null && check.length() > 0) {
        return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Invalid check value '" + check + "'" + CRLF).build();
    }
    Table table = null;
    try {
        List<RowModel> rows = model.getRows();
        List<Put> puts = new ArrayList<>();
        for (RowModel row : rows) {
            byte[] key = row.getKey();
            if (key == null) {
                key = rowspec.getRow();
            }
            if (key == null) {
                servlet.getMetrics().incrementFailedPutRequests(1);
                return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Row key not specified." + CRLF).build();
            }
            Put put = new Put(key);
            int i = 0;
            for (CellModel cell : row.getCells()) {
                byte[] col = cell.getColumn();
                if (col == null)
                    try {
                        col = rowspec.getColumns()[i++];
                    } catch (ArrayIndexOutOfBoundsException e) {
                        col = null;
                    }
                if (col == null) {
                    servlet.getMetrics().incrementFailedPutRequests(1);
                    return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request: Column found to be null." + CRLF).build();
                }
                byte[][] parts = CellUtil.parseColumn(col);
                if (parts.length != 2) {
                    return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request" + CRLF).build();
                }
                put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(put.getRow()).setFamily(parts[0]).setQualifier(parts[1]).setTimestamp(cell.getTimestamp()).setType(Type.Put).setValue(cell.getValue()).build());
            }
            puts.add(put);
            if (LOG.isTraceEnabled()) {
                LOG.trace("PUT " + put.toString());
            }
        }
        table = servlet.getTable(tableResource.getName());
        table.put(puts);
        ResponseBuilder response = Response.ok();
        servlet.getMetrics().incrementSucessfulPutRequests(1);
        return response.build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedPutRequests(1);
        return processException(e);
    } finally {
        if (table != null)
            try {
                table.close();
            } catch (IOException ioe) {
                LOG.debug("Exception received while closing the table", ioe);
            }
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put) IOException(java.io.IOException) RowModel(org.apache.hadoop.hbase.rest.model.RowModel) CellModel(org.apache.hadoop.hbase.rest.model.CellModel) ResponseBuilder(org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder)

Example 15 with ResponseBuilder

use of org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder in project hbase by apache.

the class RootResource method get.

@GET
@Produces({ MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF, MIMETYPE_PROTOBUF_IETF })
public Response get(@Context final UriInfo uriInfo) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("GET " + uriInfo.getAbsolutePath());
    }
    servlet.getMetrics().incrementRequests(1);
    try {
        ResponseBuilder response = Response.ok(getTableList());
        response.cacheControl(cacheControl);
        servlet.getMetrics().incrementSucessfulGetRequests(1);
        return response.build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedGetRequests(1);
        return processException(e);
    }
}
Also used : ResponseBuilder(org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder) IOException(java.io.IOException) Produces(org.apache.hbase.thirdparty.javax.ws.rs.Produces) GET(org.apache.hbase.thirdparty.javax.ws.rs.GET)

Aggregations

ResponseBuilder (org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder)15 IOException (java.io.IOException)12 GET (org.apache.hbase.thirdparty.javax.ws.rs.GET)11 Produces (org.apache.hbase.thirdparty.javax.ws.rs.Produces)11 CellModel (org.apache.hadoop.hbase.rest.model.CellModel)5 RowModel (org.apache.hadoop.hbase.rest.model.RowModel)5 Cell (org.apache.hadoop.hbase.Cell)4 Table (org.apache.hadoop.hbase.client.Table)4 TableNotFoundException (org.apache.hadoop.hbase.TableNotFoundException)3 ServerName (org.apache.hadoop.hbase.ServerName)2 Put (org.apache.hadoop.hbase.client.Put)2 CellSetModel (org.apache.hadoop.hbase.rest.model.CellSetModel)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ClusterMetrics (org.apache.hadoop.hbase.ClusterMetrics)1 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)1 RegionMetrics (org.apache.hadoop.hbase.RegionMetrics)1 ServerMetrics (org.apache.hadoop.hbase.ServerMetrics)1 TableExistsException (org.apache.hadoop.hbase.TableExistsException)1 TableName (org.apache.hadoop.hbase.TableName)1