001 /*
002 * Cumulus4j - Securing your data in the cloud - http://cumulus4j.org
003 * Copyright (C) 2011 NightLabs Consulting GmbH
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
017 */
018 package org.cumulus4j.keymanager.front.webapp;
019
020 import java.io.IOException;
021
022 import javax.ws.rs.Consumes;
023 import javax.ws.rs.POST;
024 import javax.ws.rs.Path;
025 import javax.ws.rs.PathParam;
026 import javax.ws.rs.Produces;
027 import javax.ws.rs.WebApplicationException;
028 import javax.ws.rs.core.MediaType;
029 import javax.ws.rs.core.Response;
030 import javax.ws.rs.core.Response.Status;
031
032 import org.cumulus4j.keymanager.front.shared.DateDependentKeyStrategyInitParam;
033 import org.cumulus4j.keymanager.front.shared.DateDependentKeyStrategyInitResult;
034 import org.cumulus4j.keymanager.front.shared.Error;
035 import org.cumulus4j.keystore.DateDependentKeyStrategy;
036 import org.cumulus4j.keystore.KeyStore;
037 import org.cumulus4j.keystore.KeyStoreNotEmptyException;
038
039 /**
040 * REST service to work with a {@link KeyStore} via the {@link DateDependentKeyStrategy}.
041 * At the moment, it only provides an initialisation method, but others might follow later.
042 *
043 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
044 */
045 @Path("DateDependentKeyStrategy")
046 @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
047 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
048 public class DateDependentKeyStrategyService extends AbstractService
049 {
050 /**
051 * Initialise a {@link KeyStore} by delegating to {@link DateDependentKeyStrategy#init(String, char[], long, long)}.
052 * @param keyStoreID identifier of the key-store to work with.
053 * @param param parameters controlling how the initialisation should behave.
054 * @return summary-result of the initialisation.
055 */
056 @Path("{keyStoreID}/init")
057 @POST
058 public DateDependentKeyStrategyInitResult init(@PathParam("keyStoreID") String keyStoreID, DateDependentKeyStrategyInitParam param)
059 {
060 DateDependentKeyStrategyInitResult result = new DateDependentKeyStrategyInitResult();
061 Auth auth = getAuth();
062 try {
063 KeyStore keyStore = keyStoreManager.getKeyStore(keyStoreID);
064 new DateDependentKeyStrategy(keyStore).init(
065 auth.getUserName(), auth.getPassword(),
066 param.getKeyActivityPeriodMSec(), param.getKeyStorePeriodMSec()
067 );
068
069 result.setGeneratedKeyCount(
070 keyStore.getKeyIDs(auth.getUserName(), auth.getPassword()).size()
071 );
072
073 return result;
074 } catch (KeyStoreNotEmptyException e) {
075 throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new Error(e)).build());
076 } catch (IOException e) {
077 throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(new Error(e)).build());
078 } catch (Exception e) {
079 throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(new Error(e)).build());
080 } finally {
081 auth.clear();
082 }
083 }
084 }