This repository has been archived by the owner on Jun 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e96c9e4
commit 7d99a7e
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
sample-app/src/main/java/com/sample/app/CustomContainerResponseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.sample.app; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerResponseContext; | ||
import javax.ws.rs.container.ContainerResponseFilter; | ||
import javax.ws.rs.ext.Provider; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Custom container request filter just to | ||
* exercise Spring beans as providers and, | ||
* specifically, in this case, as a filter. | ||
* | ||
* @author Fabio Carvalho ([email protected] or [email protected]) | ||
*/ | ||
@Component | ||
@Provider | ||
public class CustomContainerResponseFilter implements ContainerResponseFilter { | ||
|
||
@Autowired | ||
private CustomSingletonBean customSingletonBean; | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { | ||
|
||
// This will cause a NPE if this bean couldn't be injected, | ||
// and that is all we want to check. No need for assertions here | ||
customSingletonBean.amIAlive(); | ||
|
||
// Checks if request has a HTTP header named "ping". | ||
// If it does, adds an HTTP header named "pong" to the response. | ||
// The header value is irrelevant. | ||
if(requestContext.getHeaderString("ping") != null) { | ||
responseContext.getHeaders().add("pong", "pong"); | ||
} | ||
} | ||
|
||
} |