diff options
author | Martin Schreier <martin_s@apache.org> | 2022-02-13 19:22:52 +0100 |
---|---|---|
committer | Martin Schreier <martin_s@apache.org> | 2022-02-13 19:22:52 +0100 |
commit | f41b35042911a767eb3b14f37c11969ad3144fa9 (patch) | |
tree | 8f893e4f29c28a3096fdc06bb544fdc974c164ca /archiva-modules/archiva-base | |
parent | 0a2dab16770cce10bda6fa2cc2e2d9c7d64c5dbf (diff) | |
download | archiva-f41b35042911a767eb3b14f37c11969ad3144fa9.tar.gz archiva-f41b35042911a767eb3b14f37c11969ad3144fa9.zip |
Adding rest events
Diffstat (limited to 'archiva-modules/archiva-base')
7 files changed, 76 insertions, 40 deletions
diff --git a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/Event.java b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/Event.java index 2439105de..87ba24fef 100644 --- a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/Event.java +++ b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/Event.java @@ -33,7 +33,7 @@ import java.util.stream.Collectors; * Events can be chained, which means a event listener can catch events and rethrow them as its own event. * */ -public class Event extends EventObject implements Cloneable { +public class Event<C extends EventContext> extends EventObject implements Cloneable { private static final long serialVersionUID = -7171846575892044990L; @@ -95,6 +95,10 @@ public class Event extends EventObject implements Cloneable { throw new IllegalArgumentException( "No matching event context registered for " + contextClazz ); } + public C getContext() { + throw new UnsupportedOperationException( ); + } + public Map<String, String> getContextData() { return contextMap.entrySet( ).stream( ).flatMap( ctx -> ctx.getValue( ).getData( ).entrySet( ).stream( ) ) .collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue ) ); @@ -104,8 +108,8 @@ public class Event extends EventObject implements Cloneable { this.contextMap.put( clazz, context ); } - public <T extends EventContext> void setContext( T context) { - this.contextMap.put( context.getClass(), context ); + public void setContext( C context) { + throw new UnsupportedOperationException( ); } /** diff --git a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/EventContext.java b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/EventContext.java index 9e113de06..d74c82c04 100644 --- a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/EventContext.java +++ b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/EventContext.java @@ -33,10 +33,10 @@ import java.util.Map; public interface EventContext { /** - * Returns the prefix used for entry keys in the repository data map. - * @return the prefix string for this context + * Returns the id which is also used as prefix for keys in the repository data map. + * @return the identifier of this context */ - String getPrefix(); + String getId(); /** * Returns the context data as map of strings. Each entry key is prefixed with diff --git a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/EventContextBuilder.java b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/EventContextBuilder.java index 41f10ceec..0ccfc9564 100644 --- a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/EventContextBuilder.java +++ b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/EventContextBuilder.java @@ -21,6 +21,9 @@ import org.apache.archiva.event.context.RepositoryContext; import org.apache.archiva.event.context.RestContext; import org.apache.archiva.event.context.UserContext; +import java.util.List; +import java.util.Map; + /** * Static helper class that allows to set certain context data * @@ -34,8 +37,9 @@ public class EventContextBuilder evt.setContext( UserContext.class, new UserContext( user, remoteAddress ) ); } - public static void setRestcontext(Event evt, String service, String path, String operation, int resultCode, String... parameters ) { - evt.setContext( RestContext.class, new RestContext( service, path, operation, resultCode, parameters ) ); + public static void setRestContext( Event evt, String path, String service, String operation, + String requestMethod, int resultCode, Map<String, List<String>> pathParameter) { + evt.setContext( RestContext.class, new RestContext( path, service, operation, requestMethod, resultCode, pathParameter ) ); } public static void setRepositoryContext(Event evt, String id, String type, String flavour ) { @@ -56,8 +60,9 @@ public class EventContextBuilder return this; } - public EventContextBuilder witRest( String service, String path, String operation, int resultCode, String... parameters) { - setRestcontext( this.evt, service, path, operation, resultCode, parameters ); + public EventContextBuilder witRest( String path, String service, String operation, String requestMethod, + int resultCode, Map<String,List<String>> pathParameter) { + setRestContext( this.evt, path, service, operation, requestMethod, resultCode, pathParameter ); return this; } diff --git a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/RepositoryContext.java b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/RepositoryContext.java index 4df023d1b..1fb7f4843 100644 --- a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/RepositoryContext.java +++ b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/RepositoryContext.java @@ -32,15 +32,15 @@ public class RepositoryContext implements EventContext, Serializable { private static final long serialVersionUID = -4172663291198878307L; - private static final String PREFIX = "repository"; + private static final String ID = "repository"; - private final String id; + private final String repositoryId; private final String type; private final String flavour; - public RepositoryContext( String id, String type, String flavour ) + public RepositoryContext( String repositoryId, String type, String flavour ) { - this.id = id; + this.repositoryId = repositoryId; this.type = type; this.flavour = flavour; } @@ -49,9 +49,9 @@ public class RepositoryContext implements EventContext, Serializable * Returns the repository id * @return the repository id */ - public String getId( ) + public String getRepositoryId( ) { - return id; + return repositoryId; } /** @@ -76,15 +76,15 @@ public class RepositoryContext implements EventContext, Serializable public Map<String, String> getData( ) { Map<String, String> values = new HashMap<>( ); - values.put( PREFIX+".id", id ); - values.put( PREFIX+".type", type ); - values.put( PREFIX+".flavour", flavour ); + values.put( ID +".repositoryId", repositoryId ); + values.put( ID +".type", type ); + values.put( ID +".flavour", flavour ); return values; } @Override - public String getPrefix( ) + public String getId( ) { - return PREFIX; + return ID; } } diff --git a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/RestContext.java b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/RestContext.java index 615759d42..c413735a8 100644 --- a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/RestContext.java +++ b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/RestContext.java @@ -20,7 +20,6 @@ package org.apache.archiva.event.context; import org.apache.archiva.event.EventContext; import java.io.Serializable; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -34,22 +33,25 @@ public class RestContext implements EventContext, Serializable { private static final long serialVersionUID = -4109505194250928317L; - public static final String PREFIX = "rest"; + public static final String ID = "rest"; private final String service; private final String path; private final String operation; - private final List<String> parameters; + private final String requestMethod; private final int resultCode; + private final Map<String, List<String>> pathParameter; - public RestContext( String service, String path, String operation, int resultCode, String... parameters ) + public RestContext( String path, String service, String operation, String requestMethod, int resultCode, + Map<String, List<String>> pathParameter) { this.service = service; this.path = path; this.operation = operation; this.resultCode = resultCode; - this.parameters = Arrays.asList( parameters ); + this.requestMethod = requestMethod; + this.pathParameter = pathParameter; } public String getService( ) @@ -67,9 +69,9 @@ public class RestContext implements EventContext, Serializable return operation; } - public List<String> getParameters( ) + public String getRequestMethod( ) { - return parameters; + return requestMethod; } public int getResultCode( ) @@ -77,20 +79,33 @@ public class RestContext implements EventContext, Serializable return resultCode; } + public Map<String, List<String>> getPathParameter() { + return pathParameter; + } + @Override public Map<String, String> getData( ) { Map<String, String> values = new HashMap<>( ); - values.put( PREFIX+".service", service ); - values.put( PREFIX+".path", path ); - values.put( PREFIX+".operation", operation ); - values.put( PREFIX+".parameter", String.join( ",", parameters ) ); + values.put( ID +".service", service ); + values.put( ID +".path", path ); + values.put( ID +".operation", operation ); + values.put( ID +".requestMethod", requestMethod ); + values.put( ID + ".pathParameter", getParamString( ) ); return values; } @Override - public String getPrefix( ) + public String getId( ) { - return PREFIX; + return ID; + } + + private String getParamString() { + StringBuilder sb = new StringBuilder( ); + for(Map.Entry<String, List<String>> entry : pathParameter.entrySet()) { + sb.append( entry.getKey( ) ).append( String.join( ",", entry.getValue( ) ) ); + } + return sb.toString( ); } } diff --git a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/UserContext.java b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/UserContext.java index 8a83c73c8..9d3eeacdc 100644 --- a/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/UserContext.java +++ b/archiva-modules/archiva-base/archiva-event-api/src/main/java/org/apache/archiva/event/context/UserContext.java @@ -32,7 +32,7 @@ public class UserContext implements EventContext, Serializable { private static final long serialVersionUID = -3499164111736559781L; - private static final String PREFIX = "user"; + private static final String ID = "user"; private final String userId; private final String remoteAddress; @@ -58,14 +58,14 @@ public class UserContext implements EventContext, Serializable public Map<String, String> getData( ) { Map<String, String> values = new HashMap<>( ); - values.put( PREFIX+".user_id", userId ); - values.put( PREFIX+".remote_address", remoteAddress ); + values.put( ID +".user_id", userId ); + values.put( ID +".remote_address", remoteAddress ); return values; } @Override - public String getPrefix( ) + public String getId( ) { - return PREFIX; + return ID; } } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/event/RepositoryEvent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/event/RepositoryEvent.java index a4b667b3c..0bd5a0570 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/event/RepositoryEvent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/event/RepositoryEvent.java @@ -22,6 +22,7 @@ package org.apache.archiva.repository.event; import org.apache.archiva.event.Event; import org.apache.archiva.event.EventContextBuilder; import org.apache.archiva.event.EventType; +import org.apache.archiva.event.context.RepositoryContext; import org.apache.archiva.repository.ManagedRepository; import org.apache.archiva.repository.RemoteRepository; import org.apache.archiva.repository.Repository; @@ -31,7 +32,7 @@ import org.apache.archiva.repository.RepositoryGroup; * A repository event is specific to a repository and holds a reference to the repository that * is related to this event. */ -public class RepositoryEvent extends Event +public class RepositoryEvent extends Event<RepositoryContext> { private static final long serialVersionUID = 4676673476606414834L; @@ -56,6 +57,17 @@ public class RepositoryEvent extends Event } @Override + public RepositoryContext getContext() { + return getContext( RepositoryContext.class ); + } + + @Override + public void setContext( RepositoryContext context ) + { + setContext( RepositoryContext.class, context ); + } + + @Override public EventType<? extends RepositoryEvent> getType() { return (EventType<? extends RepositoryEvent>) super.getType(); } |