de.perdian.apps.dashboard.mvc.modules.pingdom.PingdomController.java Source code

Java tutorial

Introduction

Here is the source code for de.perdian.apps.dashboard.mvc.modules.pingdom.PingdomController.java

Source

/*
 * Dashboard
 * Copyright 2014 Christian Robert
 *
 * 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 de.perdian.apps.dashboard.mvc.modules.pingdom;

import java.util.LinkedHashSet;
import java.util.Set;

import org.codehaus.jackson.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import de.perdian.apps.dashboard.services.pingdom.PingdomService;
import de.perdian.apps.dashboard.support.clients.JsonUtil;

@Controller
class PingdomController {

    private PingdomService pingdomService = null;

    @RequestMapping("/pingdom/")
    @ResponseBody
    PingdomControllerResponse doPingdom(PingdomControllerRequest controllerRequest) {

        PingdomControllerResponse pingdomResponse = new PingdomControllerResponse();
        pingdomResponse.setItems(this.getPingdomService()
                .createItems(PingdomController.createIdentifierSet(controllerRequest.getIdentifiers())));
        return pingdomResponse;

    }

    // -------------------------------------------------------------------------
    // --- Helper methods ------------------------------------------------------
    // -------------------------------------------------------------------------

    private static Set<String> createIdentifierSet(String identifiers) {
        JsonNode identifiersNode = JsonUtil.fromJsonString(identifiers);
        Set<String> result = new LinkedHashSet<>();
        for (int i = 0; i < identifiersNode.size(); i++) {
            result.add(identifiersNode.get(i).asText());
        }
        return result;
    }

    // -------------------------------------------------------------------------
    // --- Property access methods ---------------------------------------------
    // -------------------------------------------------------------------------

    PingdomService getPingdomService() {
        return this.pingdomService;
    }

    @Autowired
    void setPingdomService(PingdomService pingdomService) {
        this.pingdomService = pingdomService;
    }

}