/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.cxf.fediz.service.idp;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.Document;
import org.apache.cxf.fediz.service.idp.model.IDPConfig;
import org.apache.cxf.fediz.service.idp.service.ConfigService;
import org.apache.cxf.fediz.service.idp.util.MetadataWriter;
import org.apache.ws.security.util.DOM2Writer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class MetadataServlet extends HttpServlet {
public static final String PARAM_REALM = "realm";
private static final Logger LOG = LoggerFactory
.getLogger(MetadataServlet.class);
private static final long serialVersionUID = 1L;
private ApplicationContext applicationContext;
private String realm;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
try {
ConfigService cs = (ConfigService)getApplicationContext().getBean("config");
IDPConfig idpConfig = cs.getIDPConfig(realm);
LOG.debug(idpConfig.toString());
MetadataWriter mw = new MetadataWriter();
Document metadata = mw.getMetaData(idpConfig);
out.write(DOM2Writer.nodeToString(metadata));
} catch (Exception ex) {
LOG.error("Failed to get metadata document: ", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
realm = config.getInitParameter(PARAM_REALM);
if (realm == null || realm.length() == 0) {
throw new ServletException("Servlet parameter '" + PARAM_REALM + "' not defined");
}
}
public ApplicationContext getApplicationContext() {
if (applicationContext == null) {
LOG.debug(this.getServletContext().toString());
applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
}
return applicationContext;
}
}