Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAREST-836 Provide ability to handle 'after' Http findOne event #213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed 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.springframework.data.rest.core.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Pavel Varchenko
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface HandleAfterFindOne {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed 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.springframework.data.rest.core.event;

/**
* Emitted after entity was found by Id.
*
* @author Pavel Varchenko
*/
public class AfterFindOneEvent extends RepositoryEvent {

public AfterFindOneEvent(Object source) {
super(source);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
import org.springframework.data.rest.core.annotation.HandleAfterDelete;
import org.springframework.data.rest.core.annotation.HandleAfterFindOne;
import org.springframework.data.rest.core.annotation.HandleAfterLinkDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkSave;
import org.springframework.data.rest.core.annotation.HandleAfterSave;
Expand All @@ -49,6 +50,7 @@
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Pavel Varchenko
*/
public class AnnotatedEventHandlerInvoker implements ApplicationListener<RepositoryEvent>, BeanPostProcessor {

Expand Down Expand Up @@ -135,6 +137,7 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
inspect(bean, method, HandleAfterDelete.class, AfterDeleteEvent.class);
inspect(bean, method, HandleBeforeLinkDelete.class, BeforeLinkDeleteEvent.class);
inspect(bean, method, HandleAfterLinkDelete.class, AfterLinkDeleteEvent.class);
inspect(bean, method, HandleAfterFindOne.class, AfterFindOneEvent.class);
}
}, Methods.USER_METHODS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.data.rest.core.domain.PersonRepository;
import org.springframework.data.rest.core.event.AfterCreateEvent;
import org.springframework.data.rest.core.event.AfterDeleteEvent;
import org.springframework.data.rest.core.event.AfterFindOneEvent;
import org.springframework.data.rest.core.event.AfterLinkDeleteEvent;
import org.springframework.data.rest.core.event.AfterLinkSaveEvent;
import org.springframework.data.rest.core.event.AfterSaveEvent;
Expand All @@ -48,6 +49,7 @@
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Pavel Varchenko
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
Expand Down Expand Up @@ -161,4 +163,12 @@ public void shouldDispatchBeforeLinkDelete() throws Exception {
public void shouldDispatchAfterLinkDelete() throws Exception {
appCtx.publishEvent(new AfterLinkDeleteEvent(person, new Object()));
}

/**
* @see DATAREST-836
*/
@Test(expected = EventHandlerInvokedException.class)
public void shouldDispatchAfterFindOne() throws Exception {
appCtx.publishEvent(new AfterFindOneEvent(person));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,22 @@
*/
package org.springframework.data.rest.core.domain;

import org.springframework.data.rest.core.annotation.HandleAfterCreate;
import org.springframework.data.rest.core.annotation.HandleAfterDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkSave;
import org.springframework.data.rest.core.annotation.HandleAfterSave;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkSave;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.data.rest.core.annotation.*;

/**
* Sample annotation-based event handler.
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Pavel Varchenko
*/
@RepositoryEventHandler
public class AnnotatedPersonEventHandler {

@HandleAfterCreate
@HandleAfterDelete
@HandleAfterSave
@HandleAfterFindOne
public void handleAfter(Person p) {
throw new EventHandlerInvokedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,7 @@
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.event.AfterCreateEvent;
import org.springframework.data.rest.core.event.AfterDeleteEvent;
import org.springframework.data.rest.core.event.AfterFindOneEvent;
import org.springframework.data.rest.core.event.AfterSaveEvent;
import org.springframework.data.rest.core.event.BeforeCreateEvent;
import org.springframework.data.rest.core.event.BeforeDeleteEvent;
Expand Down Expand Up @@ -75,6 +76,7 @@
* @author Oliver Gierke
* @author Greg Turnquist
* @author Jeremy Rickard
* @author Pavel Varchenko
*/
@RepositoryRestController
class RepositoryEntityController extends AbstractRepositoryRestController implements ApplicationEventPublisherAware {
Expand Down Expand Up @@ -313,6 +315,7 @@ public ResponseEntity<?> headForItemResource(RootResourceInformation resourceInf
if (domainObject == null) {
throw new ResourceNotFoundException();
}
publisher.publishEvent(new AfterFindOneEvent(domainObject));

Links links = new Links(assembler.toResource(domainObject).getLinks());

Expand Down