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

src-main.org.awakefw.sql.util.TransportConverter Maven / Gradle / Ivy

/*
 * Awake File: Easy file upload & download through HTTP with Java
 * Awake SQL: Remote JDBC access through HTTP.                                    
 * Copyright (C) 2012, Kawan Softwares S.A.S.
 * (http://www.awakeframework.org). All rights reserved.                                
 *                                                                               
 * Awake File/SQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * Awake File/SQL is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */

//TransportConverter.java
//Copyright (c) Kawan Softwares S.A.S, 2012
//
//Last Updates: 
// 27 juil. 07 16:17:50 Nicolas de Pomereu
// 13/01/09 20:10 NDP  Allow to set null values in toTransportFormat()
// 26/01/09 11:05 NDP Factorize replaced String
// 02/03/10 17:45 NDP - TransportConverter 
//                      Fix bug ==> if ( x.trim().equals("")) ==> if ( x.length()==0)
// 27/09/10 20:10 NDP - Remove unused tags & methods
// 28/09/10 17:45 NDP - Comments
// 22/09/10 10:41 NDP - TransportConverter: no more AWAKE_BLANK usage
// 11/12/11 15:55 NDP : TransportConverter: remove the to/fromHtml versions
// 11/12/11 15:55 NDP : TransportConverter: 
// 04/04/12 16:20 NDP : TransportConverter: use org.awakefw.file.api.util.Base64 class for Base64 conv

package org.awakefw.sql.util;


import org.apache.commons.lang3.StringUtils;
import org.awakefw.file.api.util.AwakeDebug;
import org.awakefw.file.api.util.Base64;
import org.awakefw.file.util.AwakeLogger;
import org.awakefw.file.util.Tag;


/**
 * @author Nicolas de Pomereu
 * 
 *         Convert a String to a transportable format, aka ascii 7 & HTML
 */
public class TransportConverter {
    /** Debug flag */
    private static boolean DEBUG = AwakeDebug.isSet(TransportConverter.class);

    public static String CR_LF = System.getProperty("line.separator");

    // public static final String AWAKE_ROOT_TAG = "**!awake_";
    // public static final String AWAKE_ROOT_TAG_ESCAPED = "";
    // public static final String AWAKE_BLANK = "**!awake_blank!**";

    public static final String AWAKE_BYTES = "**!awake_bytes!**";

    // To say there is a file to fetch from the server
    public static final String AWAKE_STREAM_FILE = "**!awake_stream_file!**";
    public static final String AWAKE_STREAM_FAILURE = "**!awake_stream_failure!**";
    public static final String AWAKE_STREAM_NULL = "**!awake_stream_null!**";

    /**
     * Constructor
     */
    protected TransportConverter() {
	// Not allowed
    }

    /**
     * Convert a byte array to a "transportable" format: - Transform it to Hex
     * value as String - Prefix it with **!awake_bytes!**"
     * 
     * @param x
     *            the byte array to transport
     * @return a byte array in transportable format
     */
    public static String toTransportFormat(byte[] x) {
	String encodedString = null;

	// Allow to set null values and "transport it":
	if (x == null) {
	    encodedString = AWAKE_BYTES + "null";
	} else {
	    encodedString = AWAKE_BYTES + Base64.byteArrayToBase64(x);
	}

	return encodedString;
    }

    /**
     * Transform a byte [] transported in hex prefixed by "**!awake_bytes!**" to
     * it's orginal byte []
     * 
     * @param string
     *            the string that contains the bytes prefixed by
     *            "**!awake_bytes!**"
     * @return the bytes
     */
    public static byte[] fromTransportFormatToBytes(String string) {
	String encodedString = StringUtils.substringAfter(string, AWAKE_BYTES);

	if (encodedString.equals("null")) {
	    return null;
	}

	try {
	    byte[] bytes = Base64.base64ToByteArray(string);
	    return bytes;
	} catch (Exception e) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "String is not in hex format: " + encodedString, e);
	}

    }

    /**
     * Convert a String to a "transportable" format:
     * 
     * - HTML for special chars. - Special delimiters are transformed into key
     * words.
     * 
     * @param x
     *            the string to transport
     * @return a srring in transportable format
     */
    /*
    public static String toTransportFormat(String x) {
	// Allow to set null values
	if (x == null)
	    return null;

	x = HtmlConverter.toHtml(x);

	// Replace all special tags with a replacement tag (defaults to "")
	// x = StringMgr.replaceAll(x, AWAKE_ROOT_TAG, AWAKE_ROOT_TAG_ESCAPED);

	// if (x.length() == 0)
	// {
	// x = AWAKE_BLANK;
	// }
	//
	return x;
    }
    */
    
    
    /**
     * Convert "transportable" formatted string to it's original format:
     * 
     * - special chars from HTML - key words transformed back into special
     * delimiters.
     * 
     * @param x
     *            the transported String
     * @return a string in it's native format
     */
    /*
    public static String fromTransportFormat(String x) {
	debug("fromTransportFormat :" + x + ":");

	x = HtmlConverter.fromHtml(x);

	return x;
    }
    */
    
    /**
     * Displays the given message if DEBUG is set.
     * 
     * @param s
     *            the debug message
     */

    @SuppressWarnings("unused")
    private static void debug(String s) {
	if (DEBUG) {
	    AwakeLogger.log(s);
	}
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy