org.mitre.secretsharing.server.JoinServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.mitre.secretsharing.server.JoinServlet.java

Source

/*
    
Copyright 2014 The MITRE Corporation
    
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.
    
This project contains content developed by The MITRE Corporation. If this 
code is used in a deployment or embedded within another project, it is 
requested that you send an email to opensource@mitre.org in order to let 
us know where this software is being used.
    
 */

package org.mitre.secretsharing.server;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

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

import org.mitre.secretsharing.Part;
import org.mitre.secretsharing.Secrets;
import org.mitre.secretsharing.codec.PartFormats;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.Base64Variants;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class JoinServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static class Request {
        public List<String> parts;
        public Boolean base64;
    }

    @JsonInclude(Include.NON_NULL)
    public static class Response {
        public String status;
        public String secret;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);

        resp.setContentType("application/json");

        try {
            Request jreq = mapper.readValue(req.getParameter("q"), Request.class);

            if (jreq.parts == null)
                throw new IllegalArgumentException();

            Part[] parts = new Part[jreq.parts.size()];
            for (int i = 0; i < parts.length; i++)
                parts[i] = PartFormats.parse(jreq.parts.get(i));

            byte[] secret = parts[0].join(Arrays.copyOfRange(parts, 1, parts.length));

            Response jresp = new Response();
            jresp.status = "ok";

            if (jreq.base64 != null && jreq.base64)
                jresp.secret = Base64Variants.MIME_NO_LINEFEEDS.encode(secret);
            else
                jresp.secret = new String(secret, "UTF-8");

            mapper.writeValue(resp.getOutputStream(), jresp);
        } catch (Throwable t) {
            t.printStackTrace();

            Response jresp = new Response();
            jresp.status = "error";

            mapper.writeValue(resp.getOutputStream(), jresp);
        }
    }
}