com.apipulse.bastion.controllers.ApiController.java Source code

Java tutorial

Introduction

Here is the source code for com.apipulse.bastion.controllers.ApiController.java

Source

/*
 * Bastion Data Ingestor
 * Copyright (c) 2016 API Fortress Inc.
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.apipulse.bastion.controllers;

import com.apipulse.bastion.actors.BastionActors;
import com.apipulse.bastion.actors.messages.StepMessage;
import org.apache.commons.io.IOUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.UUID;

/**
 * Main HTTP controller
 */
@Controller
@EnableAutoConfiguration
public class ApiController {

    @RequestMapping(value = "/api/admittance/{id}", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public String admittance(final @PathVariable String id, final HttpServletRequest request) throws IOException {
        final InputStream is = request.getInputStream();
        final String payload = IOUtils.toString(is);
        is.close();
        final StepMessage message = new StepMessage(payload);
        final HashMap<String, String> headers = new HashMap<String, String>();
        final Enumeration<String> eh = request.getHeaderNames();
        while (eh.hasMoreElements()) {
            final String headerName = eh.nextElement();
            headers.put(headerName, request.getHeader(headerName));
        }
        message.getContext().put("headers", headers);
        message.getContext().put("id", id);
        final String uuid = UUID.randomUUID().toString();
        message.getContext().put("uuid", uuid);
        BastionActors.getInstance().getHttpRouter().tell(message, null);
        return "{\"accepted\":true,\"id\":\"" + uuid + "\"}";
    }
}