net.orpiske.tcs.service.rest.controller.DomainCommandsController.java Source code

Java tutorial

Introduction

Here is the source code for net.orpiske.tcs.service.rest.controller.DomainCommandsController.java

Source

/**
 Copyright 2014 Otavio Rodolfo Piske
    
 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 net.orpiske.tcs.service.rest.controller;

import net.orpiske.tcs.service.core.domain.Domain;
import net.orpiske.tcs.service.core.events.request.RequestCreateDomainEvent;
import net.orpiske.tcs.service.core.events.response.DomainCreateEvent;
import net.orpiske.tcs.service.core.service.TagCloudService;
import org.apache.log4j.Logger;
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.*;
import org.springframework.web.util.UriComponentsBuilder;

@Controller
@RequestMapping("/domain")
public class DomainCommandsController {
    private static final Logger logger = Logger.getLogger(DomainCommandsController.class);

    @Autowired
    private TagCloudService tagCloudService;

    @RequestMapping(value = "/{domain}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Domain> createCspTagCloud(@RequestBody final Domain domain,
            UriComponentsBuilder builder) {

        if (logger.isDebugEnabled()) {
            logger.debug("CSP command controller handling a create CSP request for " + domain);
        }

        DomainCreateEvent tagCloudEvent = tagCloudService.createDomain(new RequestCreateDomainEvent(domain));

        Domain domainObj = tagCloudEvent.getDomain();

        HttpHeaders httpHeaders = new HttpHeaders();

        httpHeaders.setLocation(builder.path("/domain/{domain}").buildAndExpand(domainObj.getDomain()).toUri());

        return new ResponseEntity<Domain>(domainObj, httpHeaders, HttpStatus.OK);
    }
}