Search in sources :

Example 1 with ModelList

use of datawave.webservice.model.ModelList in project datawave by NationalSecurityAgency.

the class ModelBean method listModelNames.

/**
 * Get the names of the models
 *
 * @param modelTableName
 *            name of the table that contains the model
 * @return datawave.webservice.model.ModelList
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
 *
 * @HTTP 200 success
 * @HTTP 500 internal server error
 */
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff", "text/html" })
@Path("/list")
@GZIP
@Interceptors(ResponseInterceptor.class)
public ModelList listModelNames(@QueryParam("modelTableName") String modelTableName) {
    if (modelTableName == null) {
        modelTableName = defaultModelTableName;
    }
    ModelList response = new ModelList(jqueryUri, dataTablesUri, modelTableName);
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String user = p.getName();
    Set<Authorizations> cbAuths = new HashSet<>();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal cp = (DatawavePrincipal) p;
        user = cp.getShortName();
        for (Collection<String> auths : cp.getAuthorizations()) {
            cbAuths.add(new Authorizations(auths.toArray(new String[auths.size()])));
        }
    }
    log.trace(user + " has authorizations " + cbAuths);
    Connector connector = null;
    HashSet<String> modelNames = new HashSet<>();
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
        try (Scanner scanner = ScannerHelper.createScanner(connector, this.checkModelTableName(modelTableName), cbAuths)) {
            for (Entry<Key, Value> entry : scanner) {
                String colf = entry.getKey().getColumnFamily().toString();
                if (!RESERVED_COLF_VALUES.contains(colf) && !modelNames.contains(colf)) {
                    String[] parts = colf.split(ModelKeyParser.NULL_BYTE);
                    if (parts.length == 1)
                        modelNames.add(colf);
                    else if (parts.length == 2)
                        modelNames.add(parts[0]);
                }
            }
        }
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.MODEL_NAME_LIST_ERROR, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        if (null != connector) {
            try {
                connectionFactory.returnConnection(connector);
            } catch (Exception e) {
                log.error("Error returning connection to factory", e);
            }
        }
    }
    response.setNames(modelNames);
    return response;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) QueryException(datawave.webservice.query.exception.QueryException) ModelList(datawave.webservice.model.ModelList) QueryException(datawave.webservice.query.exception.QueryException) DefaultValue(javax.ws.rs.DefaultValue) Value(org.apache.accumulo.core.data.Value) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 2 with ModelList

use of datawave.webservice.model.ModelList in project datawave by NationalSecurityAgency.

the class ModelBeanTest method testListModels.

@Test
public void testListModels() throws Exception {
    importModels();
    PowerMock.resetAll();
    EasyMock.expect(ctx.getCallerPrincipal()).andReturn(principal);
    HashMap<String, String> trackingMap = new HashMap<>();
    EasyMock.expect(connectionFactory.getTrackingMap((StackTraceElement[]) EasyMock.anyObject())).andReturn(trackingMap);
    EasyMock.expect(connectionFactory.getConnection(EasyMock.eq(AccumuloConnectionFactory.Priority.LOW), EasyMock.eq(trackingMap))).andReturn(connector);
    connectionFactory.returnConnection(connector);
    PowerMock.replayAll();
    ModelList list = bean.listModelNames((String) null);
    PowerMock.verifyAll();
    Assert.assertEquals(2, list.getNames().size());
    Assert.assertTrue(list.getNames().contains(MODEL_ONE.getName()));
    Assert.assertTrue(list.getNames().contains(MODEL_TWO.getName()));
}
Also used : ModelList(datawave.webservice.model.ModelList) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ModelList

use of datawave.webservice.model.ModelList in project datawave by NationalSecurityAgency.

the class ModelBean method importModel.

/**
 * <strong>Administrator credentials required.</strong> Insert a new model
 *
 * @param model
 * @param modelTableName
 *            name of the table that contains the model
 * @return datawave.webservice.result.VoidResponse
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
 *
 * @HTTP 200 success
 * @HTTP 412 if model already exists with this name, delete it first
 * @HTTP 500 internal server error
 */
@POST
@Consumes({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml" })
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@Path("/import")
@GZIP
@RolesAllowed({ "Administrator", "JBossAdministrator" })
@Interceptors(ResponseInterceptor.class)
public VoidResponse importModel(datawave.webservice.model.Model model, @QueryParam("modelTableName") String modelTableName) {
    if (modelTableName == null) {
        modelTableName = defaultModelTableName;
    }
    if (log.isDebugEnabled()) {
        log.debug("modelTableName: " + (null == modelTableName ? "" : modelTableName));
    }
    VoidResponse response = new VoidResponse();
    ModelList models = listModelNames(modelTableName);
    if (models.getNames().contains(model.getName()))
        throw new PreConditionFailedException(null, response);
    insertMapping(model, modelTableName);
    return response;
}
Also used : ModelList(datawave.webservice.model.ModelList) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) VoidResponse(datawave.webservice.result.VoidResponse) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Interceptors(javax.interceptor.Interceptors) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 4 with ModelList

use of datawave.webservice.model.ModelList in project datawave by NationalSecurityAgency.

the class ModelBean method deleteModel.

private VoidResponse deleteModel(@Required("name") String name, String modelTableName, boolean reloadCache) {
    if (log.isDebugEnabled()) {
        log.debug("model name: " + name);
        log.debug("modelTableName: " + (null == modelTableName ? "" : modelTableName));
    }
    VoidResponse response = new VoidResponse();
    ModelList models = listModelNames(modelTableName);
    if (!models.getNames().contains(name))
        throw new NotFoundException(null, response);
    datawave.webservice.model.Model model = getModel(name, modelTableName);
    deleteMapping(model, modelTableName, reloadCache);
    return response;
}
Also used : ModelList(datawave.webservice.model.ModelList) VoidResponse(datawave.webservice.result.VoidResponse) NotFoundException(datawave.webservice.common.exception.NotFoundException)

Aggregations

ModelList (datawave.webservice.model.ModelList)4 NotFoundException (datawave.webservice.common.exception.NotFoundException)2 PreConditionFailedException (datawave.webservice.common.exception.PreConditionFailedException)2 VoidResponse (datawave.webservice.result.VoidResponse)2 Interceptors (javax.interceptor.Interceptors)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 GZIP (org.jboss.resteasy.annotations.GZIP)2 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)1 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)1 QueryException (datawave.webservice.query.exception.QueryException)1 Principal (java.security.Principal)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1 DefaultValue (javax.ws.rs.DefaultValue)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 Connector (org.apache.accumulo.core.client.Connector)1