com.bennavetta.appsite.webapi.SettingsController.java Source code

Java tutorial

Introduction

Here is the source code for com.bennavetta.appsite.webapi.SettingsController.java

Source

/**
 * Copyright 2013 Ben Navetta <ben@bennavetta.com>
 *
 * 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 com.bennavetta.appsite.webapi;

import java.net.URI;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import com.bennavetta.appsite.util.DatastoreConfig;

@Controller
@RequestMapping(value = "/settings", produces = "application/json")
public class SettingsController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private DatastoreConfig settings;

    @ResponseBody
    @RequestMapping(method = GET, value = "/{name}")
    public Object getSetting(@PathVariable String name) {
        Object value = settings.get(name);
        log.trace("Retrieving setting {} = {}", name, value);
        return value;
    }

    @ResponseBody
    @RequestMapping(method = GET)
    public Map<String, Object> getSettings() {
        Iterator<String> keys = settings.getKeys();
        Map<String, Object> result = new HashMap<>();
        while (keys.hasNext()) {
            String key = keys.next();
            result.put(key, settings.get(key));
        }
        log.trace("Retrieving settings {}", result);
        return result;
    }

    @RequestMapping(method = POST, value = "/{name}")
    public ResponseEntity<String> putSetting(@PathVariable String name, @RequestBody String value) {
        settings.set(name, value);
        log.trace("Setting {} = {}", name, value);
        HttpHeaders headers = new HttpHeaders();
        URI location = ServletUriComponentsBuilder.fromCurrentServletMapping().path("/settings/{name}").build()
                .expand(name).toUri();
        headers.setLocation(location);
        return new ResponseEntity<String>(headers, HttpStatus.CREATED);
    }

    @RequestMapping(method = DELETE, value = "/{name}")
    public ResponseEntity<String> deleteSetting(@PathVariable String name) {
        String oldValue = settings.getString(name);
        settings.clearProperty(name);
        log.trace("Clearing {} (was {})", name, oldValue);
        return new ResponseEntity<String>(oldValue, HttpStatus.OK);
    }
}