Search in sources :

Example 96 with ResponseBuilder

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

the class SchemaResource 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(new TableSchemaModel(getTableSchema()));
        response.cacheControl(cacheControl);
        servlet.getMetrics().incrementSucessfulGetRequests(1);
        return response.build();
    } catch (Exception e) {
        servlet.getMetrics().incrementFailedGetRequests(1);
        return processException(e);
    }
}
Also used : TableSchemaModel(org.apache.hadoop.hbase.rest.model.TableSchemaModel) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) TableNotEnabledException(org.apache.hadoop.hbase.TableNotEnabledException) TableExistsException(org.apache.hadoop.hbase.TableExistsException) IOException(java.io.IOException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 97 with ResponseBuilder

use of 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().getClusterStatus().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(javax.ws.rs.core.Response.ResponseBuilder) StorageClusterVersionModel(org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 98 with ResponseBuilder

use of 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: Either 0 or more than 1 columns specified." + 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(javax.ws.rs.core.Response.ResponseBuilder) Cell(org.apache.hadoop.hbase.Cell) IOException(java.io.IOException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 99 with ResponseBuilder

use of 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 = KeyValue.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 = KeyValue.parseColumn(col);
                if (parts.length != 2) {
                    return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request" + CRLF).build();
                }
                put.addImmutable(parts[0], parts[1], cell.getTimestamp(), cell.getValue());
                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.checkAndPut(key, valueToPutParts[0], valueToPutParts[1], valueToCheckCell.getValue(), 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(javax.ws.rs.core.Response.ResponseBuilder) Put(org.apache.hadoop.hbase.client.Put) IOException(java.io.IOException)

Example 100 with ResponseBuilder

use of 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 != 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 = KeyValue.parseColumn(col);
                if (parts.length != 2) {
                    return Response.status(Response.Status.BAD_REQUEST).type(MIMETYPE_TEXT).entity("Bad request" + CRLF).build();
                }
                put.addImmutable(parts[0], parts[1], cell.getTimestamp(), cell.getValue());
            }
            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(javax.ws.rs.core.Response.ResponseBuilder)

Aggregations

ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)175 GET (javax.ws.rs.GET)84 Produces (javax.ws.rs.Produces)81 Path (javax.ws.rs.Path)69 WebApplicationException (javax.ws.rs.WebApplicationException)47 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)40 IOException (java.io.IOException)30 MediaType (javax.ws.rs.core.MediaType)29 POST (javax.ws.rs.POST)23 IRI (org.semanticweb.owlapi.model.IRI)22 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)21 Response (javax.ws.rs.core.Response)20 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)19 URI (java.net.URI)18 Consumes (javax.ws.rs.Consumes)18 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)16 HashSet (java.util.HashSet)14 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)12