package org.vasttrafik.wso2.carbon.common.api.utils;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.ResourceBundle;

import org.vasttrafik.wso2.carbon.common.api.beans.Error;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.core.Response;

/**
 * @author Lars Andersson
 *
 */
public class ResponseUtils {
	
	public static void checkParameter(String resourceBundle, String parameterName, boolean required, String[] validValues, String value) throws BadRequestException {
		try {
			/**
			 * Check for missing required parameter value
			 */
			if (required && value == null)
				throw new Exception();

			/**
			 * Check for valid parameter value
			 */
			if (value != null) {
				if (validValues != null && validValues.length > 0) {
					for (int i = 0; i < validValues.length; i++) {
						if (validValues[i].equalsIgnoreCase(value))
							return;
					}
					throw new Exception();
				}
			}
		}
		catch (Exception e) {
			badRequest(resourceBundle, 1000L, new Object[][]{{value},{parameterName}});
		}
	}
	
	public static void preconditionFailed(String resourceBundle, Long code, Object[][] args) throws ClientErrorException {
		Error error = buildError(resourceBundle, code, args);
		Response response = Response.status(Response.Status.PRECONDITION_FAILED).entity(error).build();
		
		throw new ClientErrorException(response);
	}
	
	public static void badRequest(String resourceBundle, Long code, Object[][] args) throws BadRequestException {
		Error error = buildError(resourceBundle, code, args);
		Response response = Response.status(Response.Status.BAD_REQUEST).entity(error).build();

		throw new BadRequestException(response);
	}
	
	public static String getErrorMessage(String resourceBundle, String error, Object[] args) {
		AbstractErrorListResourceBundle bundle = (AbstractErrorListResourceBundle)
				ResourceBundle.getBundle(resourceBundle, Locale.getDefault(), ResponseUtils.class.getClassLoader());
		
		if (bundle != null) {
			try {
				return bundle.getMessage(error, args);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		return error;
	}
	
	public static Error buildError(String resourceBundle, Long code, Object[][] args) {
		AbstractErrorListResourceBundle bundle = (AbstractErrorListResourceBundle)
				ResourceBundle.getBundle(resourceBundle, Locale.getDefault(), ResponseUtils.class.getClassLoader());
		
		if (bundle != null) {
			try {
				return bundle.getError(code, args);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		return new Error();
	}
	
	public static Response buildError(int status, Long code, String message, String description, String moreInfo) {
		Error error = new Error();
		error.setCode(code);
		error.setMessage(message);
		error.setDescription(description);
		error.setMoreInfo(moreInfo);
		return Response.status(status).entity(error).build();
	}
	
	public static Response serverError(Exception e) {
		Error error = new Error();
		error.setMessage(e.getMessage());
		
		Throwable t = e.getCause();
		
		if (t != null) {
			OutputStream os = new ByteArrayOutputStream();
			PrintWriter writer = new PrintWriter(os);
			t.printStackTrace(writer);
			error.setDescription("Cause:" + t.getMessage());
			error.setMoreInfo(writer.toString());
		}

		return Response.status(500).entity(error).build();
	}
}

@@ -441,16 +441,77 @@
 eUtils %7B
+%0A%0A    private static Locale SWEDISH = new Locale(%22sv%22, %22SE%22);
 %0A%09%0A%09publ
@@ -2059,35 +2059,23 @@
 Bundle, 
-Locale.getDefault()
+SWEDISH
 , Respon
@@ -2482,27 +2482,15 @@
 le, 
-Locale.getDefault()
+SWEDISH
 , Re

Set Locale to swedish until we support multiple locales.