io.ignitr.dispatchr.manager.controller.topic.TopicController.java Source code

Java tutorial

Introduction

Here is the source code for io.ignitr.dispatchr.manager.controller.topic.TopicController.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.topic;

import io.ignitr.dispatchr.manager.core.error.TopicNotFoundException;
import io.ignitr.dispatchr.manager.domain.topic.FindTopicResponse;
import io.ignitr.dispatchr.manager.domain.topic.RegisterTopicResponse;
import io.ignitr.dispatchr.manager.domain.topic.UnregisterTopicResponse;
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.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 used to find and associate SNS topics with Dispatchr.
 */
@RestController
@RequestMapping("/v1/topics/{name}")
public class TopicController {

    @Autowired
    private TopicService service;

    @Autowired
    private ObservableErrorHandler errorHandler;

    /**
     * Get the metadata for a specific topic.
     *
     * @param topicName the name of the topic for which to retrieve metadata
     * @return an HTTP 200 response if the request was successful
     */
    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public DeferredResult<ResponseEntity<FindTopicResponse>> findOne(@PathVariable("name") String topicName,
            HttpServletRequest httpRequest) {
        final DeferredResult<ResponseEntity<FindTopicResponse>> deferredResult = new DeferredResult<>();

        service.findOne(topicName).lift(new RequestContextStashOperator<>()).last().map(topicMetadata -> {
            if (topicMetadata == null) {
                throw new TopicNotFoundException(topicName);
            }

            return topicMetadata;
        }).map(FindTopicResponse::from).subscribeOn(Schedulers.io()).subscribe(body -> {
            deferredResult.setResult(ResponseEntity.ok(body));
        }, error -> {
            deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error));
        });

        return deferredResult;
    }

    /**
     * Checks whether or not the supplied topic is registered with Dispatchr.
     *
     * @param topicName the name of the topic to check
     * @param httpRequest http request
     * @return an HTTP 204 response if the topic is registered; otherwise an HTTP 404 if the topic is not registered
     */
    @RequestMapping(method = RequestMethod.GET, value = "/registered", produces = MediaType.APPLICATION_JSON_VALUE)
    public DeferredResult<ResponseEntity<?>> isRegistered(@PathVariable("name") String topicName,
            HttpServletRequest httpRequest) {
        final DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>();

        service.isRegistered(topicName).lift(new RequestContextStashOperator<>()).last()
                .subscribeOn(Schedulers.io()).subscribe(isRegistered -> {
                    if (isRegistered) {
                        deferredResult.setResult(ResponseEntity.noContent().build());
                    } else {
                        deferredResult.setResult(ResponseEntity.notFound().build());
                    }
                }, error -> {
                    deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error));
                });

        return deferredResult;
    }

    /**
     * Register an SNS topic with Dispatchr.
     *
     * @param topicName the name of the topic to save
     * @param httpRequest http request
     * @return an HTTP 201 response if the request was successful
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/registered", produces = MediaType.APPLICATION_JSON_VALUE)
    public DeferredResult<ResponseEntity<?>> register(@PathVariable("name") String topicName,
            HttpServletRequest httpRequest) {
        final DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>();

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

        return deferredResult;
    }

    /**
     * Unregister an SNS topic with Dispatchr.
     *
     * @param topicName the name of the topic to delete
     * @param httpRequest http request
     * @return an HTTP 204 response if the request was successful
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "/registered", produces = MediaType.APPLICATION_JSON_VALUE)
    public DeferredResult<ResponseEntity<?>> unregister(@PathVariable("name") String topicName,
            HttpServletRequest httpRequest) {
        final DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>();

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

        return deferredResult;
    }
}