001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2016 ForgeRock AS. 015 */ 016package org.opends.server.protocols.http.rest2ldap; 017 018import static org.forgerock.json.resource.http.CrestHttp.newHttpHandler; 019import static org.forgerock.opendj.rest2ldap.Rest2LdapJsonConfigurator.configureEndpoint; 020import static org.forgerock.util.Options.defaultOptions; 021import static org.opends.messages.ConfigMessages.ERR_CONFIG_REST2LDAP_INVALID; 022import static org.opends.messages.ConfigMessages.ERR_CONFIG_REST2LDAP_UNABLE_READ; 023import static org.opends.messages.ConfigMessages.ERR_CONFIG_REST2LDAP_UNEXPECTED_JSON; 024import static org.opends.server.util.StaticUtils.getFileForPath; 025import static org.opends.server.util.StaticUtils.stackTraceToSingleLineString; 026 027import java.io.File; 028import java.io.IOException; 029 030import org.forgerock.http.Handler; 031import org.forgerock.http.HttpApplication; 032import org.forgerock.http.HttpApplicationException; 033import org.forgerock.http.io.Buffer; 034import org.forgerock.json.JsonValueException; 035import org.forgerock.opendj.server.config.server.Rest2ldapEndpointCfg; 036import org.forgerock.util.Factory; 037import org.opends.server.api.HttpEndpoint; 038import org.opends.server.core.ServerContext; 039import org.opends.server.protocols.http.LocalizedHttpApplicationException; 040import org.opends.server.types.InitializationException; 041 042/** 043 * Encapsulates configuration required to start a REST2LDAP application embedded 044 * in this LDAP server. Acts as a factory for {@link HttpApplication}. 045 */ 046public final class Rest2LdapEndpoint extends HttpEndpoint<Rest2ldapEndpointCfg> 047{ 048 049 /** 050 * Create a new Rest2LdapEndpoint with the supplied configuration. 051 * 052 * @param configuration 053 * Configuration to use for the {@link HttpApplication} 054 * @param serverContext 055 * Server of this LDAP server 056 */ 057 public Rest2LdapEndpoint(Rest2ldapEndpointCfg configuration, ServerContext serverContext) 058 { 059 super(configuration, serverContext); 060 } 061 062 @Override 063 public HttpApplication newHttpApplication() throws InitializationException 064 { 065 return new InternalRest2LDAPHttpApplication(); 066 } 067 068 /** 069 * Specialized {@link HttpApplication} using internal connections to this local LDAP server. 070 */ 071 private final class InternalRest2LDAPHttpApplication implements HttpApplication 072 { 073 @Override 074 public Handler start() throws HttpApplicationException 075 { 076 final File endpointConfig = getFileForPath(configuration.getConfigDirectory(), serverContext); 077 try 078 { 079 return newHttpHandler(configureEndpoint(endpointConfig, defaultOptions())); 080 } 081 catch (IOException e) 082 { 083 throw new LocalizedHttpApplicationException(ERR_CONFIG_REST2LDAP_UNABLE_READ.get( 084 endpointConfig, configuration.dn(), stackTraceToSingleLineString(e)), e); 085 } 086 catch (JsonValueException e) 087 { 088 throw new LocalizedHttpApplicationException(ERR_CONFIG_REST2LDAP_UNEXPECTED_JSON.get( 089 e.getJsonValue().getPointer(), endpointConfig, configuration.dn(), stackTraceToSingleLineString(e)), e); 090 } 091 catch (IllegalArgumentException e) 092 { 093 throw new LocalizedHttpApplicationException(ERR_CONFIG_REST2LDAP_INVALID.get( 094 endpointConfig, configuration.dn(), stackTraceToSingleLineString(e)), e); 095 } 096 } 097 098 @Override 099 public void stop() 100 { 101 // Nothing to do 102 } 103 104 @Override 105 public Factory<Buffer> getBufferFactory() 106 { 107 return null; 108 } 109 } 110}