Java tutorial
/* * Copyright 2016 Greg Whitaker * * 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 io.ignitr.dispatchr.manager.controller.topic; import io.ignitr.dispatchr.manager.core.validation.SortDirectionValidator; import io.ignitr.dispatchr.manager.domain.internal.Topic; import io.ignitr.dispatchr.manager.domain.topic.FindTopicsResponse; import io.ignitr.dispatchr.manager.service.TopicService; import io.ignitr.springboot.common.error.ObservableErrorHandler; import io.ignitr.springboot.common.rx.RequestContextStashOperator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.async.DeferredResult; import rx.Observable; import rx.Subscriber; import rx.schedulers.Schedulers; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; /** * Controller used to find and associate SNS topics with Dispatchr. */ @RestController @RequestMapping("/v1/topics") public class TopicsController { @Autowired private TopicService service; @Autowired private ObservableErrorHandler errorHandler; /** * Find all available SNS topics within the AWS account and whether or not they are registered with Dispatchr. * * @param offset index of item to start returning when using pagination * @param limit number of items to return when using pagination * @param sortDir sort order in which to return items when using pagination * @return an HTTP 200 response if the request was successful */ @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public DeferredResult<ResponseEntity<FindTopicsResponse>> findAll( @RequestParam(value = "offset", defaultValue = "0") Long offset, @RequestParam(value = "limit", defaultValue = "25") Long limit, @RequestParam(value = "sort_dir", defaultValue = "asc") String sortDir, HttpServletRequest httpRequest) { final DeferredResult<ResponseEntity<FindTopicsResponse>> deferredResult = new DeferredResult<>(); Observable.fromCallable(() -> SortDirectionValidator.validate(sortDir)) .lift(new RequestContextStashOperator<>()) .flatMap(valid -> Observable.create(new Observable.OnSubscribe<List<Topic>>() { List<Topic> topicMetadatas = new ArrayList<>(); @Override public void call(Subscriber<? super List<Topic>> subscriber) { service.findAll(offset, limit, sortDir).collect(() -> topicMetadatas, List::add) .subscribe(topicMetadatas -> { subscriber.onNext(topicMetadatas); subscriber.onCompleted(); }); } })).map(FindTopicsResponse::from).subscribeOn(Schedulers.io()).subscribe(body -> { deferredResult.setResult(ResponseEntity.ok(body)); }, error -> { deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error)); }); return deferredResult; } /** * Find all SNS topics that have been registered with Dispatchr. * * @param offset index of item to start returning when using pagination * @param limit number of items to return when using pagination * @param sortDir sort order in which to return items when using pagination * @return an HTTP 200 response if the request was successful */ @RequestMapping(method = RequestMethod.GET, value = "/registered", produces = MediaType.APPLICATION_JSON_VALUE) public DeferredResult<ResponseEntity<FindTopicsResponse>> findRegistered( @RequestParam(value = "offset", defaultValue = "0") Long offset, @RequestParam(value = "limit", defaultValue = "25") Long limit, @RequestParam(value = "sort_dir", defaultValue = "asc") String sortDir, HttpServletRequest httpRequest) { final DeferredResult<ResponseEntity<FindTopicsResponse>> deferredResult = new DeferredResult<>(); Observable.fromCallable(() -> SortDirectionValidator.validate(sortDir)) .lift(new RequestContextStashOperator<>()) .flatMap(valid -> Observable.create(new Observable.OnSubscribe<List<Topic>>() { List<Topic> topicMetadatas = new ArrayList<>(); @Override public void call(Subscriber<? super List<Topic>> subscriber) { service.findRegistered(offset, limit, sortDir).collect(() -> topicMetadatas, List::add) .subscribe(topicMetadatas -> { subscriber.onNext(topicMetadatas); subscriber.onCompleted(); }); } })).map(FindTopicsResponse::from).subscribeOn(Schedulers.io()).subscribe(body -> { deferredResult.setResult(ResponseEntity.ok(body)); }, error -> { deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error)); }); return deferredResult; } /** * Find all SNS topics that are not registered with Dispatchr. * * @param offset index of item to start returning when using pagination * @param limit number of items to return when using pagination * @param sortDir sort order in which to return items when using pagination * @return an HTTP 200 response if the request was successful */ @RequestMapping(method = RequestMethod.GET, value = "/unregistered", produces = MediaType.APPLICATION_JSON_VALUE) public DeferredResult<ResponseEntity<FindTopicsResponse>> findUnregistered( @RequestParam(value = "offset", defaultValue = "0") Long offset, @RequestParam(value = "limit", defaultValue = "25") Long limit, @RequestParam(value = "sort_dir", defaultValue = "asc") String sortDir, HttpServletRequest httpRequest) { final DeferredResult<ResponseEntity<FindTopicsResponse>> deferredResult = new DeferredResult<>(); Observable.fromCallable(() -> SortDirectionValidator.validate(sortDir)) .lift(new RequestContextStashOperator<>()) .flatMap(valid -> Observable.create(new Observable.OnSubscribe<List<Topic>>() { List<Topic> topicMetadatas = new ArrayList<>(); @Override public void call(Subscriber<? super List<Topic>> subscriber) { service.findUnregistered(offset, limit, sortDir).collect(() -> topicMetadatas, List::add) .subscribe(topicMetadatas -> { subscriber.onNext(topicMetadatas); subscriber.onCompleted(); }); } })).map(FindTopicsResponse::from).subscribeOn(Schedulers.io()).subscribe(body -> { deferredResult.setResult(ResponseEntity.ok(body)); }, error -> { deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error)); }); return deferredResult; } }