Last active
August 27, 2020 22:45
-
-
Save lightoze/32f6f43b4a8fd8f3a12c2d288489f07c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package testapp; | |
import io.micronaut.runtime.Micronaut; | |
public class TestApp { | |
public static void main(String[] args) { | |
Micronaut.run(TestApp.class); | |
} | |
} |
This file contains hidden or 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
package testapp; | |
import io.micronaut.http.HttpHeaders; | |
import io.micronaut.http.HttpResponse; | |
import io.micronaut.http.HttpStatus; | |
import io.micronaut.http.MutableHttpResponse; | |
import io.micronaut.http.annotation.Controller; | |
import io.micronaut.http.annotation.Get; | |
import io.reactivex.Flowable; | |
import org.reactivestreams.Publisher; | |
@Controller | |
public class TestController { | |
@Get("/test") | |
public Publisher<MutableHttpResponse<?>> test() { | |
MutableHttpResponse<?> response = HttpResponse.status(HttpStatus.FOUND); | |
response.header(HttpHeaders.LOCATION, "/redirect"); | |
return Flowable.just(response); | |
} | |
@Get("/body") | |
public Publisher<MutableHttpResponse<?>> testBody() { | |
MutableHttpResponse<?> response = HttpResponse.ok("Body"); | |
return Flowable.just(response); | |
} | |
@Get("/content") | |
public Publisher<String> testContent() { | |
return Flowable.just("Content"); | |
} | |
@Get("/block") | |
public MutableHttpResponse<?> testBlock() { | |
MutableHttpResponse<?> response = HttpResponse.status(HttpStatus.FOUND); | |
response.header(HttpHeaders.LOCATION, "/redirect"); | |
return response; | |
} | |
} |
This file contains hidden or 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
package testapp; | |
import io.micronaut.http.HttpRequest; | |
import io.micronaut.http.MutableHttpResponse; | |
import io.micronaut.http.annotation.Filter; | |
import io.micronaut.http.cookie.Cookie; | |
import io.micronaut.http.filter.OncePerRequestHttpServerFilter; | |
import io.micronaut.http.filter.ServerFilterChain; | |
import io.reactivex.Flowable; | |
import org.reactivestreams.Publisher; | |
@Filter("/**") | |
public class TestFilter extends OncePerRequestHttpServerFilter { | |
@Override | |
protected Publisher<MutableHttpResponse<?>> doFilterOnce(HttpRequest<?> request, ServerFilterChain chain) { | |
return Flowable.fromPublisher(chain.proceed(request)) | |
.switchMap(response -> { | |
response.cookie(Cookie.of("test", "cookie")); | |
return Flowable.just(response); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment