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

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

/* *********************************************************************
 * Class JsonView
 * 
 * Copyright (c) 2011-2012, 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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.codehaus.jackson.impl.Utf8Generator;
import org.codehaus.jackson.map.MappingJsonFactory;
import org.nbnResolving.converters.JsonConverter;
import org.nbnResolving.pidef.PidefDocument;
import org.nbnResolving.resolver.controller.error.ErrorHandler;

/**
 * Class to generate JSON response to user resolving request. 
 */
public class JsonView extends BaseView {
	
    private static final Log LOGGER = LogFactory.getLog(JsonView.class);
	
	private static final MappingJsonFactory FACTORY = new org.codehaus.jackson.map.MappingJsonFactory();
	private static final org.codehaus.jackson.map.ObjectMapper MAPPER = FACTORY.getCodec();
	private static final java.text.SimpleDateFormat SDF = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
	
	private JsonConverter jsonConverter;

	static{
		MAPPER.getSerializationConfig().without(org.codehaus.jackson.map.SerializationConfig.Feature.AUTO_DETECT_IS_GETTERS);
		MAPPER.getSerializationConfig().withSerializationInclusion(org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_NULL);
		MAPPER.getSerializationConfig().withDateFormat(SDF);
	}
	
	/** Constructor */
	public JsonView(){
		setContentType("application/json");		
	}

	/**
	 * Gets the converter 
	 * @return Converter
	 */
	public JsonConverter getJsonConverter() {
		return this.jsonConverter;
	}
	
	/** 
	 * Sets the converter 
	 * @param converter
	 */
	public void setJsonConverter(final JsonConverter converter) {
		this.jsonConverter = converter;
	}


	@Override
	protected void renderMergedOutputModel(final Map model, final HttpServletRequest request,
			                               final HttpServletResponse response) throws Exception {
		
	    Utf8Generator generator = null;
		try {
		    generator = (Utf8Generator) FACTORY.createJsonGenerator(response.getOutputStream(), org.codehaus.jackson.JsonEncoding.UTF8);    
		} catch (Exception e) {
		    LOGGER.error("JsonView: Exception: " + e.getMessage());
		    if (generator != null) generator.close();
		    return;
		}
		
		for (Map.Entry entry : model.entrySet()) {
			if (entry.getValue() instanceof PidefDocument){
				PidefDocument doc = (PidefDocument) entry.getValue();
				final String requestUrl = doc.getPidef().getHeader().getRequest();
				final String providerId = doc.getPidef().getHeader().getSource(); 
				try{
					this.jsonConverter.addRecordToModel(generator, doc, SDF);
				} catch (Exception e) {
					final String message = e.getMessage();
					LOGGER.error("JsonView: Exception: " + message);
					//error! print the error text & error message
					response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
					doc = ErrorHandler.getErrorDocument(requestUrl, providerId, e);
					this.jsonConverter.addRecordToModel(generator, doc, SDF);
				} finally {
					generator.writeEndObject(); //whole object
					generator.close();
					LOGGER.debug("Closed the JsonGenerator.");
				}
				return;	
			}
		}
		if (generator != null) generator.close();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy