Commit 8d8b0b71 by Suma Shivaprasad

Fixed api response codes and audit filter logging

parent 2cbd25cc
...@@ -257,6 +257,8 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -257,6 +257,8 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
((TitanVertex) instanceVertex) ((TitanVertex) instanceVertex)
.addProperty(Constants.TRAIT_NAMES_PROPERTY_KEY, traitName); .addProperty(Constants.TRAIT_NAMES_PROPERTY_KEY, traitName);
} catch (RepositoryException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
throw new RepositoryException(e); throw new RepositoryException(e);
} }
...@@ -279,7 +281,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -279,7 +281,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
List<String> traitNames = getTraitNames(instanceVertex); List<String> traitNames = getTraitNames(instanceVertex);
if (!traitNames.contains(traitNameToBeDeleted)) { if (!traitNames.contains(traitNameToBeDeleted)) {
throw new RepositoryException("Could not find trait=" + traitNameToBeDeleted throw new EntityNotFoundException("Could not find trait=" + traitNameToBeDeleted
+ " in the repository for entity: " + guid); + " in the repository for entity: " + guid);
} }
...@@ -302,6 +304,8 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -302,6 +304,8 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
updateTraits(instanceVertex, traitNames); updateTraits(instanceVertex, traitNames);
} }
} }
} catch (RepositoryException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
throw new RepositoryException(e); throw new RepositoryException(e);
} }
...@@ -350,6 +354,8 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -350,6 +354,8 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
instanceToGraphMapper.mapAttributesToVertex(getIdFromVertex(typeName, instanceVertex), instanceToGraphMapper.mapAttributesToVertex(getIdFromVertex(typeName, instanceVertex),
instance, instanceVertex, new HashMap<Id, Vertex>(), instance, instanceVertex, new HashMap<Id, Vertex>(),
attributeInfo, attributeInfo.dataType()); attributeInfo, attributeInfo.dataType());
} catch (RepositoryException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
throw new RepositoryException(e); throw new RepositoryException(e);
} }
......
...@@ -21,9 +21,11 @@ package org.apache.hadoop.metadata.services; ...@@ -21,9 +21,11 @@ package org.apache.hadoop.metadata.services;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.inject.Provider; import com.google.inject.Provider;
import com.sun.jersey.api.NotFoundException;
import org.apache.hadoop.metadata.MetadataException; import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.MetadataServiceClient; import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.ParamChecker; import org.apache.hadoop.metadata.ParamChecker;
import org.apache.hadoop.metadata.TypeNotFoundException;
import org.apache.hadoop.metadata.classification.InterfaceAudience; import org.apache.hadoop.metadata.classification.InterfaceAudience;
import org.apache.hadoop.metadata.discovery.SearchIndexer; import org.apache.hadoop.metadata.discovery.SearchIndexer;
import org.apache.hadoop.metadata.listener.EntityChangeListener; import org.apache.hadoop.metadata.listener.EntityChangeListener;
...@@ -293,7 +295,7 @@ public class DefaultMetadataService implements MetadataService { ...@@ -293,7 +295,7 @@ public class DefaultMetadataService implements MetadataService {
// verify if the type exists // verify if the type exists
if (!typeSystem.isRegistered(entityType)) { if (!typeSystem.isRegistered(entityType)) {
throw new MetadataException("type is not defined for : " + entityType); throw new TypeNotFoundException("type is not defined for : " + entityType);
} }
} }
...@@ -327,8 +329,12 @@ public class DefaultMetadataService implements MetadataService { ...@@ -327,8 +329,12 @@ public class DefaultMetadataService implements MetadataService {
final String traitName = traitInstance.getTypeName(); final String traitName = traitInstance.getTypeName();
// ensure trait type is already registered with the TS // ensure trait type is already registered with the TS
Preconditions.checkArgument(typeSystem.isRegistered(traitName), if ( !typeSystem.isRegistered(traitName) ) {
"trait=%s should be defined in type system before it can be added", traitName); String msg = String.format("trait=%s should be defined in type system before it can be added", traitName);
LOG.error(msg);
throw new TypeNotFoundException(msg);
}
// ensure trait is not already defined // ensure trait is not already defined
Preconditions.checkArgument(!getTraitNames(guid).contains(traitName), Preconditions.checkArgument(!getTraitNames(guid).contains(traitName),
"trait=%s is already defined for entity=%s", traitName, guid); "trait=%s is already defined for entity=%s", traitName, guid);
...@@ -350,6 +356,8 @@ public class DefaultMetadataService implements MetadataService { ...@@ -350,6 +356,8 @@ public class DefaultMetadataService implements MetadataService {
TraitType traitType = typeSystem.getDataType(TraitType.class, entityTypeName); TraitType traitType = typeSystem.getDataType(TraitType.class, entityTypeName);
return traitType.convert( return traitType.convert(
traitInstance, Multiplicity.REQUIRED); traitInstance, Multiplicity.REQUIRED);
} catch ( TypeNotFoundException e ) {
throw e;
} catch (Exception e) { } catch (Exception e) {
throw new MetadataException("Error deserializing trait instance", e); throw new MetadataException("Error deserializing trait instance", e);
} }
...@@ -369,9 +377,12 @@ public class DefaultMetadataService implements MetadataService { ...@@ -369,9 +377,12 @@ public class DefaultMetadataService implements MetadataService {
ParamChecker.notEmpty(traitNameToBeDeleted, "Trait name cannot be null"); ParamChecker.notEmpty(traitNameToBeDeleted, "Trait name cannot be null");
// ensure trait type is already registered with the TS // ensure trait type is already registered with the TS
Preconditions.checkArgument(typeSystem.isRegistered(traitNameToBeDeleted), if ( !typeSystem.isRegistered(traitNameToBeDeleted)) {
"trait=%s should be defined in type system before it can be deleted", final String msg = String.format("trait=%s should be defined in type system before it can be deleted",
traitNameToBeDeleted); traitNameToBeDeleted);
LOG.error(msg);
throw new TypeNotFoundException(msg);
}
repository.deleteTrait(guid, traitNameToBeDeleted); repository.deleteTrait(guid, traitNameToBeDeleted);
......
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.metadata;
/**
* A simple wrapper for 404.
*/
public class TypeNotFoundException extends MetadataException {
public TypeNotFoundException() {
}
public TypeNotFoundException(String message) {
super(message);
}
public TypeNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public TypeNotFoundException(Throwable cause) {
super(cause);
}
public TypeNotFoundException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
...@@ -22,6 +22,7 @@ import com.google.common.collect.ArrayListMultimap; ...@@ -22,6 +22,7 @@ import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import org.apache.hadoop.metadata.MetadataException; import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.TypeNotFoundException;
import org.apache.hadoop.metadata.classification.InterfaceAudience; import org.apache.hadoop.metadata.classification.InterfaceAudience;
import org.apache.hadoop.metadata.typesystem.TypesDef; import org.apache.hadoop.metadata.typesystem.TypesDef;
...@@ -160,7 +161,7 @@ public class TypeSystem { ...@@ -160,7 +161,7 @@ public class TypeSystem {
return cls.cast(dT); return cls.cast(dT);
} }
throw new MetadataException(String.format("Unknown datatype: %s", name)); throw new TypeNotFoundException(String.format("Unknown datatype: %s", name));
} }
public StructType defineStructType(String name, public StructType defineStructType(String name,
......
...@@ -81,11 +81,12 @@ public class AuditFilter implements Filter { ...@@ -81,11 +81,12 @@ public class AuditFilter implements Filter {
final String who = getUserFromRequest(httpRequest); final String who = getUserFromRequest(httpRequest);
final String fromHost = httpRequest.getRemoteHost(); final String fromHost = httpRequest.getRemoteHost();
final String fromAddress = httpRequest.getRemoteAddr(); final String fromAddress = httpRequest.getRemoteAddr();
final String whatRequest = httpRequest.getMethod();
final String whatURL = Servlets.getRequestURL(httpRequest); final String whatURL = Servlets.getRequestURL(httpRequest);
final String whatAddrs = httpRequest.getLocalAddr(); final String whatAddrs = httpRequest.getLocalAddr();
LOG.debug("Audit: {}/{} performed request {} ({}) at time {}", LOG.debug("Audit: {}/{} performed request {} {} ({}) at time {}",
who, fromAddress, whatURL, whatAddrs, whenISO9601); who, fromAddress, whatRequest, whatURL, whatAddrs, whenISO9601);
audit(who, fromAddress, fromHost, whatURL, whatAddrs, whenISO9601); audit(who, fromAddress, fromHost, whatURL, whatAddrs, whenISO9601);
} }
......
...@@ -22,6 +22,7 @@ import com.google.common.base.Preconditions; ...@@ -22,6 +22,7 @@ import com.google.common.base.Preconditions;
import org.apache.hadoop.metadata.MetadataException; import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.MetadataServiceClient; import org.apache.hadoop.metadata.MetadataServiceClient;
import org.apache.hadoop.metadata.ParamChecker; import org.apache.hadoop.metadata.ParamChecker;
import org.apache.hadoop.metadata.TypeNotFoundException;
import org.apache.hadoop.metadata.repository.EntityNotFoundException; import org.apache.hadoop.metadata.repository.EntityNotFoundException;
import org.apache.hadoop.metadata.services.MetadataService; import org.apache.hadoop.metadata.services.MetadataService;
import org.apache.hadoop.metadata.typesystem.types.ValueConversionException; import org.apache.hadoop.metadata.typesystem.types.ValueConversionException;
...@@ -48,7 +49,6 @@ import javax.ws.rs.core.Context; ...@@ -48,7 +49,6 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
...@@ -300,11 +300,11 @@ public class EntityResource { ...@@ -300,11 +300,11 @@ public class EntityResource {
response.put(MetadataServiceClient.GUID, guid); response.put(MetadataServiceClient.GUID, guid);
return Response.created(locationURI).entity(response).build(); return Response.created(locationURI).entity(response).build();
} catch (EntityNotFoundException e) { } catch (EntityNotFoundException | TypeNotFoundException e) {
LOG.error("An entity with GUID={} does not exist", guid, e); LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException( throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.NOT_FOUND)); Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
} catch (MetadataException | IOException | IllegalArgumentException e) { } catch (MetadataException | IllegalArgumentException e) {
LOG.error("Unable to add trait for entity={}", guid, e); LOG.error("Unable to add trait for entity={}", guid, e);
throw new WebApplicationException( throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST)); Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
...@@ -338,7 +338,7 @@ public class EntityResource { ...@@ -338,7 +338,7 @@ public class EntityResource {
response.put(TRAIT_NAME, traitName); response.put(TRAIT_NAME, traitName);
return Response.ok(response).build(); return Response.ok(response).build();
} catch (EntityNotFoundException e) { } catch (EntityNotFoundException | TypeNotFoundException e) {
LOG.error("An entity with GUID={} does not exist", guid, e); LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException( throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.NOT_FOUND)); Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
......
...@@ -424,7 +424,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT { ...@@ -424,7 +424,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
.accept(Servlets.JSON_MEDIA_TYPE) .accept(Servlets.JSON_MEDIA_TYPE)
.type(Servlets.JSON_MEDIA_TYPE) .type(Servlets.JSON_MEDIA_TYPE)
.method(HttpMethod.POST, ClientResponse.class, traitInstanceAsJSON); .method(HttpMethod.POST, ClientResponse.class, traitInstanceAsJSON);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Assert.assertEquals(clientResponse.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
} }
@Test(dependsOnMethods = "testGetTraitNames") @Test(dependsOnMethods = "testGetTraitNames")
...@@ -494,7 +494,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT { ...@@ -494,7 +494,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
.accept(Servlets.JSON_MEDIA_TYPE) .accept(Servlets.JSON_MEDIA_TYPE)
.type(Servlets.JSON_MEDIA_TYPE) .type(Servlets.JSON_MEDIA_TYPE)
.method(HttpMethod.POST, ClientResponse.class, traitInstanceAsJSON); .method(HttpMethod.POST, ClientResponse.class, traitInstanceAsJSON);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Assert.assertEquals(clientResponse.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
} }
@Test(dependsOnMethods = "testAddTrait") @Test(dependsOnMethods = "testAddTrait")
...@@ -532,7 +532,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT { ...@@ -532,7 +532,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
.accept(Servlets.JSON_MEDIA_TYPE) .accept(Servlets.JSON_MEDIA_TYPE)
.type(Servlets.JSON_MEDIA_TYPE) .type(Servlets.JSON_MEDIA_TYPE)
.method(HttpMethod.DELETE, ClientResponse.class); .method(HttpMethod.DELETE, ClientResponse.class);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Assert.assertEquals(clientResponse.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class); String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString); 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