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.back.shared; 019 020 import java.util.Arrays; 021 import java.util.Collections; 022 import java.util.HashSet; 023 import java.util.Set; 024 025 import javax.ws.rs.ext.ContextResolver; 026 import javax.ws.rs.ext.Provider; 027 import javax.xml.bind.JAXBContext; 028 029 import org.slf4j.Logger; 030 import org.slf4j.LoggerFactory; 031 032 /** 033 * {@link ContextResolver} implementation telling JAXB all DTO classes that are required for 034 * the communication channel between app-server and key-manager. 035 * 036 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de 037 */ 038 @Provider 039 public final class JAXBContextResolver implements ContextResolver<JAXBContext> 040 { 041 private static final Logger logger = LoggerFactory.getLogger(JAXBContextResolver.class); 042 043 private final JAXBContext context; 044 045 private static final Class<?>[] cTypes = { 046 ErrorResponse.class, 047 GetActiveEncryptionKeyRequest.class, 048 GetActiveEncryptionKeyResponse.class, 049 GetKeyRequest.class, 050 GetKeyResponse.class, 051 NullResponse.class, 052 Request.class, 053 Response.class, 054 Message.class 055 }; 056 057 private static final Set<Class<?>> types = Collections.unmodifiableSet(new HashSet<Class<?>>(Arrays.asList(cTypes))); 058 059 /** 060 * Create a new instance of <code>JAXBContextResolver</code>. This is called by Jersey (the class is passed to it). 061 * @throws Exception in case creation of this resolver fails. 062 */ 063 public JAXBContextResolver() throws Exception { 064 logger.debug("Instantiating JAXBContextResolver."); 065 this.context = JAXBContext.newInstance(cTypes); 066 } 067 068 @Override 069 public JAXBContext getContext(Class<?> objectType) { 070 JAXBContext result = (types.contains(objectType)) ? context : null; 071 logger.debug( 072 "getContext: objectType={} matching={}", 073 (objectType == null ? null : objectType.getName()), 074 result != null 075 ); 076 return result; 077 } 078 }