package com.collective.celos.servlet;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.collective.celos.BucketID;
import com.collective.celos.RegisterKey;
import com.collective.celos.Scheduler;
import com.collective.celos.StateDatabaseConnection;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;

public class RegisterServlet extends AbstractJSONServlet {
    
    private static final String BUCKET_PARAM = "bucket";
    private static final String KEY_PARAM = "key";

    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException {
        try {
            BucketID bucket = getRequestBucketID(req);
            RegisterKey key = getRequestKey(req);
            Scheduler scheduler = getOrCreateCachedScheduler();
            try(StateDatabaseConnection connection = scheduler.getStateDatabase().openConnection()) {
                JsonNode value = connection.getRegister(bucket, key);
                if (value == null) {
                    res.sendError(HttpServletResponse.SC_NOT_FOUND, "Register not found");
                } else {
                    writer.writeValue(res.getOutputStream(), value);
                }
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
    
    protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException {
        try {
            BucketID bucket = getRequestBucketID(req);
            RegisterKey key = getRequestKey(req);
            JsonNode value = mapper.readTree(new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8));
            Scheduler scheduler = getOrCreateCachedScheduler();
            try(StateDatabaseConnection connection = scheduler.getStateDatabase().openConnection()) {
                connection.putRegister(bucket, key, value);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
    
    protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException {
        BucketID bucket = getRequestBucketID(req);
        RegisterKey key = getRequestKey(req);
        Scheduler scheduler;
        try {
            scheduler = getOrCreateCachedScheduler();
            try(StateDatabaseConnection connection = scheduler.getStateDatabase().openConnection()) {
                connection.deleteRegister(bucket, key);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
    
    private BucketID getRequestBucketID(HttpServletRequest req) {
        return new BucketID(req.getParameter(BUCKET_PARAM));
    }
    
    private RegisterKey getRequestKey(HttpServletRequest req) {
        return new RegisterKey(req.getParameter(KEY_PARAM));
    }
    
}

@@ -1,8 +1,607 @@
+/*%0A * Copyright 2015 Collective, Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *   http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or%0A * implied.  See the License for the specific language governing%0A * permissions and limitations under the License.%0A */%0A
 package 
@@ -635,36 +635,8 @@
 t;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -955,24 +955,24 @@
 .Scheduler;%0A
+
 import com.c
@@ -1016,67 +1016,8 @@
 on;%0A
-import com.fasterxml.jackson.core.JsonProcessingException;%0A
 impo

Add more license headers