io.ignitr.dispatchr.manager.controller.client.ClientController.java Source code

Java tutorial

Introduction

Here is the source code for io.ignitr.dispatchr.manager.controller.client.ClientController.java

Source

/*
 * 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.domain.client.FindClientResponse;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import rx.schedulers.Schedulers;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller responsible for managing clients.
 */
@RestController
@RequestMapping("/v1/clients/{clientId}")
public class ClientController {
    private static final Logger LOG = LoggerFactory.getLogger(ClientController.class);

    @Autowired
    private ClientService service;

    @Autowired
    private ObservableErrorHandler errorHandler;

    /**
     * Finds a specific client.
     *
     * @param clientId client identifier
     * @return an HTTP 200 response containing the client metadata or an HTTP 404 if the client does not exist
     */
    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public DeferredResult<ResponseEntity<FindClientResponse>> findOne(@PathVariable("clientId") String clientId,
            HttpServletRequest httpRequest) {
        final DeferredResult<ResponseEntity<FindClientResponse>> deferredResult = new DeferredResult<>();

        service.findOne(clientId).lift(new RequestContextStashOperator<>()).last().map(FindClientResponse::from)
                .subscribeOn(Schedulers.io()).subscribe(body -> {
                    deferredResult.setResult(ResponseEntity.ok(body));
                }, error -> {
                    deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error));
                });

        return deferredResult;
    }

    /**
     * Deletes a specific client.
     *
     * @param clientId client identifier
     * @return an HTTP 204 response if the client was deleted or an HTTP 404 if the client does not exist
     */
    @RequestMapping(method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public DeferredResult<ResponseEntity<?>> unregister(@PathVariable("clientId") String clientId,
            HttpServletRequest httpRequest) {
        final DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>();

        service.unregister(clientId).lift(new RequestContextStashOperator<>()).last().subscribeOn(Schedulers.io())
                .subscribe(complete -> {
                    deferredResult.setResult(ResponseEntity.noContent().build());
                }, error -> {
                    deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error));
                });

        return deferredResult;
    }
}