All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nbnResolving.views.XmlView Maven / Gradle / Ivy

Go to download

Java classes providing resolving functionality for Persistent Identifiers. Main focus is on National Bibliography Numbers, but some other known systems are also supported. See the official URN:NBN Resolver http://nbn-resolving.org or http://persid.org

The newest version!
/* *********************************************************************
 * Class XmlView
 * 
 * Copyright (c) 2011-2013, German National Library/Deutsche Nationalbibliothek
 * Adickesallee 1, D-60322 Frankfurt am Main, Federal Republic of Germany 
 *
 * This program is free software.
 * 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.
 * 
 * Thomas Haidlas -- German National Library
 * Kadir Karaca Kocer -- German National Library
 **********************************************************************/

/* ********************************************************************
 * CHANGELOG:
 * 
 * 2012-04-03 Port to Maven by Timo Heck & Karaca Kocer
 * 2011-12-12 Licence information and JavaDocs by Karaca Kocer
 * Created on 2011-12-08 by Thomas Haidlas -- German National Library
 ********************************************************************/

package org.nbnResolving.views;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlOptions;
import org.nbnResolving.pidef.PidefDocument;
import org.nbnResolving.resolver.controller.error.ErrorHandler;

/**
 * Class to generate XML response to user resolving request. 
 */
public class XmlView extends BaseView {
    private static final Log LOGGER = LogFactory.getLog(XmlView.class);
	private static final XmlOptions XML_OPTIONS = new org.apache.xmlbeans.XmlOptions();
	
	static {
		XML_OPTIONS.setCharacterEncoding("UTF-8");
		XML_OPTIONS.setSaveNamespacesFirst();
		XML_OPTIONS.setValidateStrict();
		XML_OPTIONS.setSavePrettyPrint();
		XML_OPTIONS.setSaveOuter();

		//options.setSaveImplicitNamespaces(implicitNamespaces)
		java.util.HashMap suggestedPrefixes = new java.util.HashMap();
		suggestedPrefixes.put("http://nbn-resolving.org/pidef", "pidef");
		XML_OPTIONS.setSaveSuggestedPrefixes(suggestedPrefixes);
	}

	/** Constructor*/
	public XmlView(){
		setContentType("text/xml");		
	}
	
	@Override
	protected void renderMergedOutputModel(final Map model, final HttpServletRequest request,
			                               final HttpServletResponse response) throws Exception {
		
		for (Map.Entry entry : model.entrySet()) {
			if (entry.getValue() instanceof PidefDocument) {
				PidefDocument doc = (PidefDocument) entry.getValue();
				
				//add the schemaLocation. See: http://wiki.apache.org/xmlbeans/XmlBeansFaq#xsiSchemaLocation
				XmlCursor cursor = doc.newCursor();
				if (cursor.toFirstChild()) {
					cursor.insertComment("-\nThis is a computer generated response to your request listed below." +
							" Do NOT modify it manually!" +
							"\nIn case of a problem see the XML Schema Definition (http://nbn-resolving.org/schemas/pidef/1.0/pidef.xsd) first." +
					        "\nKadir Karaca Ko\u00E7er - German National Library\n-");
					QName qn = new QName("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
					String location = "http://nbn-resolving.org/pidef http://nbn-resolving.org/schemas/pidef/1.0/pidef.xsd";
					cursor.setAttributeText(qn, location);
				}
				cursor.dispose();
				
				final String requestUrl = doc.getPidef().getHeader().getRequest();
				final String providerId = doc.getPidef().getHeader().getSource(); 
			
				try{
					doc.save(response.getOutputStream(), XML_OPTIONS);
				} catch (Exception e) {
					String message = e.getMessage();
					LOGGER.error("XmlView: Exception: " + message);
					//error! print the error text & error message
					response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
					doc = ErrorHandler.getErrorDocument(requestUrl, providerId, e);
					doc.save(response.getOutputStream(), XML_OPTIONS);
				}
				return;	
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy