io.ignitr.dispatchr.manager.controller.subscription.SubscriptionsController.java Source code

Java tutorial

Introduction

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

import io.ignitr.dispatchr.manager.domain.internal.Subscription;
import io.ignitr.dispatchr.manager.domain.subscription.FindSubscriptionsResponse;
import io.ignitr.dispatchr.manager.service.SubscriptionService;
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;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/v1/topics/{name}/subscriptions")
public class SubscriptionsController {

    @Autowired
    private TopicService topicService;

    @Autowired
    private SubscriptionService subscriptionService;

    @Autowired
    private ObservableErrorHandler errorHandler;

    /**
     *
     * @param topicName
     * @param httpRequest
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public DeferredResult<ResponseEntity<FindSubscriptionsResponse>> findAll(@PathVariable("name") String topicName,
            HttpServletRequest httpRequest) {
        final DeferredResult<ResponseEntity<FindSubscriptionsResponse>> deferredResult = new DeferredResult<>();

        topicService.findOne(topicName).lift(new RequestContextStashOperator<>()).flatMap(topicMetadata -> {
            List<Subscription> subscriptionMetadatas = new ArrayList<>();

            return subscriptionService.findAll(topicMetadata.getName()).collect(() -> subscriptionMetadatas,
                    List::add);
        }).map(FindSubscriptionsResponse::from).subscribeOn(Schedulers.io()).subscribe(body -> {
            deferredResult.setResult(ResponseEntity.ok(body));
        }, error -> {
            deferredResult.setErrorResult(errorHandler.handleError(httpRequest, error));
        });

        return deferredResult;
    }
}