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.client; import io.ignitr.dispatchr.manager.core.validation.SortDirectionValidator; import io.ignitr.dispatchr.manager.domain.client.FindClientsResponse; import io.ignitr.dispatchr.manager.domain.client.RegisterClientRequest; import io.ignitr.dispatchr.manager.domain.client.RegisterClientResponse; import io.ignitr.dispatchr.manager.domain.internal.Client; import io.ignitr.dispatchr.manager.service.ClientService; import io.ignitr.springboot.common.error.ObservableErrorHandler; import io.ignitr.springboot.common.rx.RequestContextStashOperator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; 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 javax.validation.Valid; import java.util.ArrayList; import java.util.List; /** * Controller responsible for managing clients. */ @RestController @RequestMapping("/v1/clients") public class ClientsController { private static final Logger LOG = LoggerFactory.getLogger(ClientsController.class); @Autowired private ClientService service; @Autowired private ObservableErrorHandler errorHandler; /** * Finds all clients in the system. * * @param offset starting offset when using pagination * @param limit number of records to return when using pagination * @param sortDir sort direction of records when using pagination * @param httpRequest http request * @return an HTTP 200 response containing a list of all clients defined in the system */ @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public DeferredResult<ResponseEntity<?>> findAll( @RequestParam(value = "offset", defaultValue = "0") Long offset, @RequestParam(value = "limit", defaultValue = "25") Long limit, @RequestParam(value = "sort_dir", defaultValue = "asc") String sortDir, @RequestParam(value = "active", defaultValue = "true") Boolean active, HttpServletRequest httpRequest) { final DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(); Observable.fromCallable(() -> SortDirectionValidator.validate(sortDir)) .lift(new RequestContextStashOperator<>()) .flatMap(valid -> Observable.create(new Observable.OnSubscribe<List<Client>>() { List<Client> clients = new ArrayList<>(); @Override public void call(Subscriber<? super List<Client>> subscriber) { service.findAll(offset, limit, sortDir, active).collect(() -> clients, List::add) .subscribe(clients -> { subscriber.onNext(clients); subscriber.onCompleted(); }); } })).map(FindClientsResponse::from).subscribeOn(Schedulers.io()).subscribe(body -> { deferredResult.setResult(ResponseEntity.ok(body)); }, error -> { deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error)); }); return deferredResult; } /** * Registers a new client in the system. * * @param request client registration request * @return an HTTP 201 response if the client was successfully registered */ @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public DeferredResult<ResponseEntity<RegisterClientResponse>> register( @Valid @RequestBody RegisterClientRequest request, HttpServletRequest httpRequest) { final DeferredResult<ResponseEntity<RegisterClientResponse>> deferredResult = new DeferredResult<>(); service.register(request).lift(new RequestContextStashOperator<>()).last().map(RegisterClientResponse::from) .subscribeOn(Schedulers.io()).subscribe(body -> { deferredResult.setResult(ResponseEntity.ok(body)); }, error -> { deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error)); }); return deferredResult; } }