Commit 223840c6 by Suma S

Merge pull request #94 from hortonworks/master

Merging from master to DAL - BUG-36928, BUG_32830, BUG-36938
parents a7483fee be0ddddc
......@@ -155,7 +155,7 @@ public class HiveMetaStoreBridge {
String entityJSON = InstanceSerialization.toJson(referenceable, true);
LOG.debug("Submitting new entity {} = {}", referenceable.getTypeName(), entityJSON);
JSONObject jsonObject = metadataServiceClient.createEntity(entityJSON);
String guid = jsonObject.getString(MetadataServiceClient.RESULTS);
String guid = jsonObject.getString(MetadataServiceClient.GUID);
LOG.debug("created instance for type " + typeName + ", guid: " + guid);
return new Referenceable(guid, referenceable.getTypeName(), null);
......
......@@ -25,10 +25,8 @@ import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.hadoop.metadata.security.SecureClientUtils;
import org.apache.hadoop.metadata.typesystem.ITypedReferenceableInstance;
import org.apache.hadoop.metadata.typesystem.Referenceable;
import org.apache.hadoop.metadata.typesystem.json.InstanceSerialization;
import org.apache.hadoop.metadata.typesystem.json.Serialization;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
......@@ -36,6 +34,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
......@@ -49,14 +48,24 @@ import static org.apache.hadoop.metadata.security.SecurityProperties.TLS_ENABLED
*/
public class MetadataServiceClient {
private static final Logger LOG = LoggerFactory.getLogger(MetadataServiceClient.class);
public static final String NAME = "name";
public static final String GUID = "GUID";
public static final String DEFINITION = "definition";
public static final String ERROR = "error";
public static final String REQUEST_ID = "requestId";
public static final String RESULTS = "results";
public static final String TOTAL_SIZE = "totalSize";
private static final String BASE_URI = "api/metadata/";
private static final String URI_TYPES = "types";
private static final String URI_ENTITIES = "entities";
private static final String URI_TRAITS = "traits";
private static final String URI_SEARCH = "discovery/search";
public static final String COUNT = "count";
public static final String ROWS = "rows";
public static final String BASE_URI = "api/metadata/";
public static final String TYPES = "types";
public static final String URI_ENTITIES = "entities";
public static final String URI_TRAITS = "traits";
public static final String URI_SEARCH = "discovery/search";
public static final String QUERY = "query";
public static final String QUERY_TYPE = "queryType";
private WebResource service;
......@@ -86,17 +95,18 @@ public class MetadataServiceClient {
}
static enum API {
//Type operations
CREATE_TYPE(BASE_URI + URI_TYPES, HttpMethod.POST),
GET_TYPE(BASE_URI + URI_TYPES, HttpMethod.GET),
LIST_TYPES(BASE_URI + URI_TYPES, HttpMethod.GET),
LIST_TRAIT_TYPES(BASE_URI + URI_TYPES + "?type=trait", HttpMethod.GET),
CREATE_TYPE(BASE_URI + TYPES, HttpMethod.POST),
GET_TYPE(BASE_URI + TYPES, HttpMethod.GET),
LIST_TYPES(BASE_URI + TYPES, HttpMethod.GET),
LIST_TRAIT_TYPES(BASE_URI + TYPES + "?type=trait", HttpMethod.GET),
//Entity operations
CREATE_ENTITY(BASE_URI + URI_ENTITIES, HttpMethod.POST),
GET_ENTITY(BASE_URI + URI_ENTITIES, HttpMethod.GET),
UPDATE_ENTITY(BASE_URI + URI_ENTITIES, HttpMethod.PUT),
LIST_ENTITY(BASE_URI + URI_ENTITIES + "?type=", HttpMethod.GET),
LIST_ENTITY(BASE_URI + URI_ENTITIES, HttpMethod.GET),
//Trait operations
ADD_TRAITS(BASE_URI + URI_TRAITS, HttpMethod.POST),
......@@ -145,7 +155,7 @@ public class MetadataServiceClient {
WebResource resource = getResource(API.GET_TYPE, typeName);
try {
JSONObject response = callAPIWithResource(API.GET_TYPE, resource);
return response.getString("definition");
return response.getString(DEFINITION);
} catch (MetadataServiceException e) {
if (e.getStatus() == ClientResponse.Status.NOT_FOUND) {
return null;
......@@ -185,7 +195,7 @@ public class MetadataServiceClient {
public Referenceable getEntity(String guid) throws MetadataServiceException {
JSONObject jsonResponse = callAPI(API.GET_ENTITY, null, guid);
try {
String entityInstanceDefinition = jsonResponse.getString(MetadataServiceClient.RESULTS);
String entityInstanceDefinition = jsonResponse.getString(MetadataServiceClient.GUID);
return InstanceSerialization.fromJsonReferenceable(entityInstanceDefinition, true);
} catch (JSONException e) {
throw new MetadataServiceException(e);
......@@ -194,7 +204,7 @@ public class MetadataServiceClient {
public JSONObject searchEntity(String searchQuery) throws MetadataServiceException {
WebResource resource = getResource(API.SEARCH);
resource = resource.queryParam("query", searchQuery);
resource = resource.queryParam(QUERY, searchQuery);
return callAPIWithResource(API.SEARCH, resource);
}
......@@ -224,10 +234,10 @@ public class MetadataServiceClient {
*/
public JSONArray searchByDSL(String query) throws MetadataServiceException {
WebResource resource = getResource(API.SEARCH_DSL);
resource = resource.queryParam("query", query);
resource = resource.queryParam(QUERY, query);
JSONObject result = callAPIWithResource(API.SEARCH_DSL, resource);
try {
return result.getJSONObject("results").getJSONArray("rows");
return result.getJSONObject(RESULTS).getJSONArray(ROWS);
} catch (JSONException e) {
throw new MetadataServiceException(e);
}
......@@ -241,7 +251,7 @@ public class MetadataServiceClient {
*/
public JSONObject searchByGremlin(String gremlinQuery) throws MetadataServiceException {
WebResource resource = getResource(API.SEARCH_GREMLIN);
resource = resource.queryParam("query", gremlinQuery);
resource = resource.queryParam(QUERY, gremlinQuery);
return callAPIWithResource(API.SEARCH_GREMLIN, resource);
}
......@@ -253,7 +263,7 @@ public class MetadataServiceClient {
*/
public JSONObject searchByFullText(String query) throws MetadataServiceException {
WebResource resource = getResource(API.SEARCH_FULL_TEXT);
resource = resource.queryParam("query", query);
resource = resource.queryParam(QUERY, query);
return callAPIWithResource(API.SEARCH_FULL_TEXT, resource);
}
......@@ -286,7 +296,9 @@ public class MetadataServiceClient {
.type(MediaType.APPLICATION_JSON)
.method(api.getMethod(), ClientResponse.class, requestObject);
if (clientResponse.getStatus() == Response.Status.OK.getStatusCode()) {
Response.Status expectedStatus = (api.getMethod() == HttpMethod.POST)
? Response.Status.CREATED : Response.Status.OK;
if (clientResponse.getStatus() == expectedStatus.getStatusCode()) {
String responseAsString = clientResponse.getEntity(String.class);
try {
return new JSONObject(responseAsString);
......
......@@ -22,6 +22,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.inject.Injector;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.discovery.SearchIndexer;
import org.apache.hadoop.metadata.listener.EntityChangeListener;
import org.apache.hadoop.metadata.listener.TypesChangeListener;
......@@ -107,18 +108,15 @@ public class DefaultMetadataService implements MetadataService {
if(typesDef.isEmpty())
throw new MetadataException("Invalid type definition");
Map<String, IDataType> typesAdded = typeSystem.defineTypes(typesDef);
final Map<String, IDataType> typesAdded = typeSystem.defineTypes(typesDef);
//TODO how do we handle transaction - store failure??
typeStore.store(typeSystem, ImmutableList.copyOf(typesAdded.keySet()));
onTypesAddedToRepo(typesAdded);
JSONObject response = new JSONObject();
for (Map.Entry<String, IDataType> entry : typesAdded.entrySet()) {
response.put(entry.getKey(), entry.getValue().getName());
}
JSONObject response = new JSONObject() {{
put(MetadataServiceClient.TYPES, typesAdded.keySet());
}};
return response;
} catch (JSONException e) {
LOG.error("Unable to create response for types={}", typeDefinition, e);
......
......@@ -31,6 +31,7 @@ import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.PropertiesUtil;
import org.apache.hadoop.metadata.RepositoryMetadataModule;
import org.apache.hadoop.metadata.repository.typestore.ITypeStore;
......@@ -83,7 +84,7 @@ public class GuiceServletConfig extends GuiceServletContextListener {
Map<String, String> params = new HashMap<>();
params.put(PackagesResourceConfig.PROPERTY_PACKAGES, packages);
serve("/api/metadata/*").with(GuiceContainer.class, params);
serve("/" + MetadataServiceClient.BASE_URI + "*").with(GuiceContainer.class, params);
}
private void configureAuthenticationFilter() throws ConfigurationException {
......
......@@ -33,12 +33,12 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.*;
import java.io.IOException;
import java.net.URI;
import java.util.List;
/**
* Entity management operations as REST API.
*
......@@ -50,12 +50,13 @@ import java.util.List;
public class EntityResource {
private static final Logger LOG = LoggerFactory.getLogger(EntityResource.class);
private static final String GUID = "GUID";
private static final String TRAIT_NAME = "traitName";
private final MetadataService metadataService;
@Context
UriInfo uriInfo;
/**
* Created by the Guice ServletModule and injected with the
* configured MetadataService.
......@@ -79,11 +80,16 @@ public class EntityResource {
LOG.debug("submitting entity {} ", entity);
final String guid = metadataService.createEntity(entity);
UriBuilder ub = uriInfo.getAbsolutePathBuilder();
URI locationURI = ub.path(guid).build();
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put(MetadataServiceClient.RESULTS, guid);
response.put(MetadataServiceClient.GUID, guid);
response.put(MetadataServiceClient.DEFINITION, entity);
return Response.ok(response).build();
return Response.created(locationURI).entity(response).build();
} catch (MetadataException | IOException | IllegalArgumentException e) {
LOG.error("Unable to persist entity instance", e);
throw new WebApplicationException(
......@@ -112,30 +118,26 @@ public class EntityResource {
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put(GUID, guid);
response.put(MetadataServiceClient.GUID, guid);
Response.Status status = Response.Status.NOT_FOUND;
if (entityDefinition != null) {
response.put(MetadataServiceClient.RESULTS, entityDefinition);
response.put(MetadataServiceClient.DEFINITION, entityDefinition);
status = Response.Status.OK;
} else {
response.put(MetadataServiceClient.ERROR, JSONObject.quote(String.format("An entity with GUID={%s} does not exist", guid)));
}
return Response.status(status).entity(response).build();
} catch (MetadataException | IllegalArgumentException e) {
LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException(e, Response
.status(Response.Status.NOT_FOUND)
.entity(e.getMessage())
.type(MediaType.APPLICATION_JSON)
.build());
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
} catch (JSONException e) {
LOG.error("Unable to get instance definition for GUID {}", guid, e);
throw new WebApplicationException(e, Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(e.getMessage())
.type(MediaType.APPLICATION_JSON)
.build());
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
}
......@@ -143,14 +145,10 @@ public class EntityResource {
* Gets the list of entities for a given entity type.
*
* @param entityType name of a type which is unique
* @param offset starting offset for pagination
* @param resultsPerPage number of results for pagination
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getEntityListByType(@QueryParam("type") String entityType,
@DefaultValue("0") @QueryParam("offset") Integer offset,
@QueryParam("numResults") Integer resultsPerPage) {
public Response getEntityListByType(@QueryParam("type") String entityType) {
Preconditions.checkNotNull(entityType, "Entity type cannot be null");
try {
LOG.debug("Fetching entity list for type={} ", entityType);
......@@ -160,7 +158,7 @@ public class EntityResource {
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put("type", entityType);
response.put(MetadataServiceClient.RESULTS, new JSONArray(entityList));
response.put(MetadataServiceClient.TOTAL_SIZE, entityList.size());
response.put(MetadataServiceClient.COUNT, entityList.size());
return Response.ok(response).build();
} catch (MetadataException | IllegalArgumentException e) {
......@@ -223,9 +221,9 @@ public class EntityResource {
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put(GUID, guid);
response.put(MetadataServiceClient.GUID, guid);
response.put(MetadataServiceClient.RESULTS, new JSONArray(traitNames));
response.put(MetadataServiceClient.TOTAL_SIZE, traitNames.size());
response.put(MetadataServiceClient.COUNT, traitNames.size());
return Response.ok(response).build();
} catch (MetadataException | IllegalArgumentException e) {
......@@ -257,12 +255,15 @@ public class EntityResource {
LOG.debug("Adding trait={} for entity={} ", traitDefinition, guid);
metadataService.addTrait(guid, traitDefinition);
UriBuilder ub = uriInfo.getAbsolutePathBuilder();
URI locationURI = ub.path(guid).build();
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put(GUID, guid);
response.put("traitInstance", traitDefinition);
response.put(MetadataServiceClient.GUID, guid);
response.put(MetadataServiceClient.DEFINITION, traitDefinition);
return Response.ok(response).build();
return Response.created(locationURI).entity(response).build();
} catch (MetadataException | IOException | IllegalArgumentException e) {
LOG.error("Unable to add trait for entity={}", guid, e);
throw new WebApplicationException(
......@@ -296,7 +297,7 @@ public class EntityResource {
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put(GUID, guid);
response.put(MetadataServiceClient.GUID, guid);
response.put(TRAIT_NAME, traitName);
return Response.ok(response).build();
......
......@@ -6,9 +6,9 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
......@@ -49,6 +49,9 @@ import java.util.Map;
public class MetadataDiscoveryResource {
private static final Logger LOG = LoggerFactory.getLogger(EntityResource.class);
private static final String QUERY_TYPE_DSL = "dsl";
private static final String QUERY_TYPE_GREMLIN = "gremlin";
private static final String QUERY_TYPE_FULLTEXT = "full-text";
private final DiscoveryService discoveryService;
......@@ -79,23 +82,23 @@ public class MetadataDiscoveryResource {
return searchUsingGremlinQuery(query);
}
try {
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put("query", query);
JSONObject response = null;
try { // fall back to dsl
final String jsonResult = discoveryService.searchByDSL(query);
response.put("queryType", "dsl");
response.put(MetadataServiceClient.RESULTS, new JSONObject(jsonResult));
final String jsonResultStr = discoveryService.searchByDSL(query);
response = new DSLJSONResponseBuilder().results(jsonResultStr)
.query(query)
.build();
} catch (Throwable throwable) {
LOG.error("Unable to get entity list for query {} using dsl", query, throwable);
try { //fall back to full-text
final String jsonResult = discoveryService.searchByFullText(query);
response.put("queryType", "full-text");
response.put(MetadataServiceClient.RESULTS, new JSONObject(jsonResult));
final String jsonResultStr = discoveryService.searchByFullText(query);
response = new FullTextJSonResponseBuilder().results(jsonResultStr)
.query(query)
.build();
} catch (DiscoveryException e) {
LOG.error("Unable to get entity list for query {}", query, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
......@@ -106,11 +109,9 @@ public class MetadataDiscoveryResource {
}
}
return Response.ok(response).build();
} catch (JSONException e) {
LOG.error("Unable to get entity list for query {}", query, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
return Response.ok(response)
.build();
}
/**
......@@ -126,15 +127,14 @@ public class MetadataDiscoveryResource {
Preconditions.checkNotNull(dslQuery, "dslQuery cannot be null");
try {
final String jsonResult = discoveryService.searchByDSL(dslQuery);
final String jsonResultStr = discoveryService.searchByDSL(dslQuery);
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put("query", dslQuery);
response.put("queryType", "dsl");
response.put(MetadataServiceClient.RESULTS, new JSONObject(jsonResult));
JSONObject response = new DSLJSONResponseBuilder().results(jsonResultStr)
.query(dslQuery)
.build();
return Response.ok(response).build();
return Response.ok(response)
.build();
} catch (DiscoveryException e) {
LOG.error("Unable to get entity list for dslQuery {}", dslQuery, e);
throw new WebApplicationException(
......@@ -164,17 +164,18 @@ public class MetadataDiscoveryResource {
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put("query", gremlinQuery);
response.put("queryType", "gremlin");
response.put(MetadataServiceClient.QUERY, gremlinQuery);
response.put(MetadataServiceClient.QUERY_TYPE, QUERY_TYPE_GREMLIN);
JSONArray list = new JSONArray();
for (Map<String, String> result : results) {
list.put(new JSONObject(result));
}
response.put(MetadataServiceClient.RESULTS, list);
response.put(MetadataServiceClient.TOTAL_SIZE, list.length());
response.put(MetadataServiceClient.COUNT, list.length());
return Response.ok(response).build();
return Response.ok(response)
.build();
} catch (DiscoveryException e) {
LOG.error("Unable to get entity list for gremlinQuery {}", gremlinQuery, e);
throw new WebApplicationException(
......@@ -199,15 +200,14 @@ public class MetadataDiscoveryResource {
Preconditions.checkNotNull(query, "query cannot be null");
try {
final String jsonResult = discoveryService.searchByFullText(query);
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put("query", query);
response.put("queryType", "full-text");
response.put(MetadataServiceClient.RESULTS, new JSONArray(jsonResult));
final String jsonResultStr = discoveryService.searchByFullText(query);
JSONArray rowsJsonArr = new JSONArray(jsonResultStr);
return Response.ok(response).build();
JSONObject response = new FullTextJSonResponseBuilder().results(rowsJsonArr)
.query(query)
.build();
return Response.ok(response)
.build();
} catch (DiscoveryException e) {
LOG.error("Unable to get entity list for query {}", query, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
......@@ -215,6 +215,104 @@ public class MetadataDiscoveryResource {
LOG.error("Unable to get entity list for query {}", query, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
}
private class JsonResponseBuilder {
protected int count = 0;
protected String query;
protected String queryType;
protected JSONObject response;
JsonResponseBuilder() {
this.response = new JSONObject();
}
protected JsonResponseBuilder count(int count) {
this.count = count;
return this;
}
public JsonResponseBuilder query(String query) {
this.query = query;
return this;
}
public JsonResponseBuilder queryType(String queryType) {
this.queryType = queryType;
return this;
}
protected JSONObject build() throws JSONException {
Preconditions.checkNotNull(query, "Query cannot be null");
Preconditions.checkNotNull(queryType, "Query Type must be specified");
Preconditions.checkArgument(count >= 0, "Search Result count should be > 0");
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
response.put(MetadataServiceClient.QUERY, query);
response.put(MetadataServiceClient.QUERY_TYPE, queryType);
response.put(MetadataServiceClient.COUNT, count);
return response;
}
}
private class DSLJSONResponseBuilder extends JsonResponseBuilder {
DSLJSONResponseBuilder() {
super();
}
private JSONObject dslResults;
public DSLJSONResponseBuilder results(JSONObject dslResults) {
this.dslResults = dslResults;
return this;
}
public DSLJSONResponseBuilder results(String dslResults) throws JSONException {
return results(new JSONObject(dslResults));
}
@Override
public JSONObject build() throws JSONException {
Preconditions.checkNotNull(dslResults);
JSONArray rowsJsonArr = dslResults.getJSONArray(MetadataServiceClient.ROWS);
count(rowsJsonArr.length());
queryType(QUERY_TYPE_DSL);
JSONObject response = super.build();
response.put(MetadataServiceClient.RESULTS, dslResults);
return response;
}
}
private class FullTextJSonResponseBuilder extends JsonResponseBuilder {
private JSONArray fullTextResults;
public FullTextJSonResponseBuilder results(JSONArray fullTextResults) {
this.fullTextResults = fullTextResults;
return this;
}
public FullTextJSonResponseBuilder results(String dslResults) throws JSONException {
return results(new JSONArray(dslResults));
}
public FullTextJSonResponseBuilder() {
super();
}
@Override
public JSONObject build() throws JSONException {
Preconditions.checkNotNull(fullTextResults);
count(fullTextResults.length());
queryType(QUERY_TYPE_FULLTEXT);
JSONObject response = super.build();
response.put(MetadataServiceClient.RESULTS, fullTextResults);
return response;
}
}
}
\ No newline at end of file
......@@ -165,7 +165,7 @@ public class RexsterGraphResource {
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.RESULTS, new JSONObject(vertexProperties));
response.put(MetadataServiceClient.TOTAL_SIZE, vertexProperties.size());
response.put(MetadataServiceClient.COUNT, vertexProperties.size());
return Response.ok(response).build();
} catch (JSONException e) {
throw new WebApplicationException(
......@@ -276,7 +276,7 @@ public class RexsterGraphResource {
if (!countOnly) {
response.put(MetadataServiceClient.RESULTS, elementArray);
}
response.put(MetadataServiceClient.TOTAL_SIZE, counter);
response.put(MetadataServiceClient.COUNT, counter);
return Response.ok(response).build();
}
......@@ -323,7 +323,7 @@ public class RexsterGraphResource {
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.RESULTS, vertexArray);
response.put(MetadataServiceClient.TOTAL_SIZE, counter);
response.put(MetadataServiceClient.COUNT, counter);
return response;
}
......
......@@ -19,10 +19,14 @@
package org.apache.hadoop.metadata.web.resources;
import com.google.common.base.Preconditions;
import com.google.inject.Guice;
import com.sun.jersey.api.client.ClientResponse;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.services.MetadataService;
import org.apache.hadoop.metadata.typesystem.types.DataTypes;
import org.apache.hadoop.metadata.typesystem.types.IDataType;
import org.apache.hadoop.metadata.web.listeners.GuiceServletConfig;
import org.apache.hadoop.metadata.web.util.Servlets;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
......@@ -34,10 +38,12 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This class provides RESTful API for Types.
......@@ -74,13 +80,22 @@ public class TypesResource {
final String typeDefinition = Servlets.getRequestPayload(request);
LOG.debug("creating type with definition {} ", typeDefinition);
JSONObject typesAdded = metadataService.createType(typeDefinition);
JSONObject typesJson = metadataService.createType(typeDefinition);
final JSONArray typesJsonArray = typesJson.getJSONArray(MetadataServiceClient.TYPES);
List<Map<String, String>> typesAddedList = new ArrayList<>();
for (int i = 0; i < typesJsonArray.length(); i++) {
final String name = typesJsonArray.getString(i);
typesAddedList.add(
new HashMap<String, String>() {{
put(MetadataServiceClient.NAME, name);
}});
}
JSONObject response = new JSONObject();
response.put("types", typesAdded);
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
return Response.ok(response).build();
response.put(MetadataServiceClient.TYPES, typesAddedList);
return Response.status(ClientResponse.Status.CREATED).entity(response).build();
} catch (Exception e) {
LOG.error("Unable to persist types", e);
throw new WebApplicationException(
......@@ -103,7 +118,7 @@ public class TypesResource {
JSONObject response = new JSONObject();
response.put("typeName", typeName);
response.put("definition", typeDefinition);
response.put(MetadataServiceClient.DEFINITION, typeDefinition);
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
return Response.ok(response).build();
......@@ -136,11 +151,11 @@ public class TypesResource {
JSONObject response = new JSONObject();
response.put(MetadataServiceClient.RESULTS, new JSONArray(result));
response.put(MetadataServiceClient.TOTAL_SIZE, result.size());
response.put(MetadataServiceClient.COUNT, result.size());
response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId());
return Response.ok(response).build();
} catch(IllegalArgumentException ie) {
} catch (IllegalArgumentException ie) {
LOG.error("Unsupported typeName while retrieving type list {}", type);
throw new WebApplicationException(
Servlets.getErrorResponse("Unsupported type " + type, Response.Status.BAD_REQUEST));
......
......@@ -20,7 +20,11 @@ package org.apache.hadoop.metadata.web.util;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.metadata.MetadataServiceClient;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;
......@@ -33,6 +37,7 @@ import java.io.StringWriter;
*/
public final class Servlets {
private static final Logger LOG = LoggerFactory.getLogger(Servlets.class);
private Servlets() {
/* singleton */
}
......@@ -97,9 +102,17 @@ public final class Servlets {
}
public static Response getErrorResponse(String message, Response.Status status) {
JSONObject errorJson = new JSONObject();
Object errorEntity = JSONObject.quote(message);
try {
errorJson.put(MetadataServiceClient.ERROR, errorEntity);
errorEntity = errorJson;
} catch (JSONException jsonE) {
LOG.warn("Could not construct error Json rensponse");
}
return Response
.status(status)
.entity(JSONObject.quote(message))
.entity(errorEntity)
.type(MediaType.APPLICATION_JSON)
.build();
}
......
......@@ -76,7 +76,7 @@ public abstract class BaseResourceIT {
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.POST, ClientResponse.class, typesAsJSON);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
Assert.assertEquals(clientResponse.getStatus(), Response.Status.CREATED.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
......@@ -93,7 +93,7 @@ public abstract class BaseResourceIT {
String entityJSON = InstanceSerialization.toJson(referenceable, true);
System.out.println("Submitting new entity= " + entityJSON);
JSONObject jsonObject = serviceClient.createEntity(entityJSON);
String guid = jsonObject.getString(MetadataServiceClient.RESULTS);
String guid = jsonObject.getString(MetadataServiceClient.GUID);
System.out.println("created instance for type " + typeName + ", guid: " + guid);
// return the reference to created instance with guid
......
......@@ -147,7 +147,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID));
final String definition = response.getString(MetadataServiceClient.RESULTS);
final String definition = response.getString(MetadataServiceClient.DEFINITION);
Assert.assertNotNull(definition);
LOG.debug("tableInstanceAfterGet = " + definition);
InstanceSerialization.fromJsonReferenceable(definition, true);
......@@ -176,7 +176,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
private String getEntityDefinition(ClientResponse clientResponse) throws Exception {
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
JSONObject response = new JSONObject(clientResponse.getEntity(String.class));
final String definition = response.getString(MetadataServiceClient.RESULTS);
final String definition = response.getString(MetadataServiceClient.DEFINITION);
Assert.assertNotNull(definition);
return definition;
......@@ -196,6 +196,9 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get(MetadataServiceClient.ERROR));
}
@Test(dependsOnMethods = "testSubmitEntity")
......@@ -232,6 +235,9 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get(MetadataServiceClient.ERROR));
}
@Test
......@@ -311,15 +317,15 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.POST, ClientResponse.class, traitInstanceAsJSON);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
Assert.assertEquals(clientResponse.getStatus(), Response.Status.CREATED.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID));
Assert.assertNotNull(response.get("GUID"));
Assert.assertNotNull(response.get("traitInstance"));
Assert.assertNotNull(response.get(MetadataServiceClient.GUID));
Assert.assertNotNull(response.get(MetadataServiceClient.DEFINITION));
}
@Test
......@@ -383,6 +389,12 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
.method(HttpMethod.DELETE, ClientResponse.class);
Assert.assertEquals(clientResponse.getStatus(),
Response.Status.BAD_REQUEST.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get(MetadataServiceClient.ERROR));
}
private void createHiveTypes() throws Exception {
......
......@@ -83,8 +83,12 @@ public class MetadataDiscoveryJerseyResourceIT extends BaseResourceIT {
JSONObject results = response.getJSONObject(MetadataServiceClient.RESULTS);
Assert.assertNotNull(results);
JSONArray rows = results.getJSONArray("rows");
JSONArray rows = results.getJSONArray(MetadataServiceClient.ROWS);
Assert.assertEquals(rows.length(), 1);
int numRows = response.getInt(MetadataServiceClient.COUNT);
Assert.assertEquals(numRows, 1);
}
@Test
......@@ -164,6 +168,9 @@ public class MetadataDiscoveryJerseyResourceIT extends BaseResourceIT {
Assert.assertNotNull(row.get("guid"));
Assert.assertEquals(row.getString("typeName"), "dsl_test_type");
Assert.assertNotNull(row.get("score"));
int numRows = response.getInt(MetadataServiceClient.COUNT);
Assert.assertEquals(numRows, 1);
}
private void createTypes() throws Exception {
......
......@@ -76,7 +76,7 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.POST, ClientResponse.class, typesAsJSON);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
Assert.assertEquals(clientResponse.getStatus(), Response.Status.CREATED.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment