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

org.kawanfw.sql.transport.UrlTransporter Maven / Gradle / Ivy

Go to download

AceQL HTTP is a framework of REST like http APIs that allow to access to remote SQL databases over http from any device that supports http. AceQL HTTP is provided with four client SDK: - The AceQL C# Client SDK allows to wrap the HTTP APIs using Microsoft SQL Server like calls in their code, just like they would for a local database. - The AceQL Java Client SDK allows to wrap the HTTP APIs using JDBC calls in their code, just like they would for a local database. - The AceQL Python Client SDK allows SQL calls to be encoded with standard unmodified DB-API 2.0 syntax

There is a newer version: 12.2
Show newest version
/*
 * Copyright (c)2022 KawanSoft S.A.S. All rights reserved.
 * 
 * Use of this software is governed by the Business Source License included
 * in the LICENSE.TXT file in the project's root directory.
 *
 * Change Date: 2026-11-01
 *
 * On the date above, in accordance with the Business Source License, use
 * of this software will be governed by version 2.0 of the Apache License.
 */
package org.kawanfw.sql.transport;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;

import org.apache.commons.lang3.StringUtils;
import org.kawanfw.sql.util.Base64;

/**
 * @author Nicolas de Pomereu
 *
 *         Allows to transform with serialization an URL to Base64 String and
 *         vice versa.
 */
public class UrlTransporter {

    public static final String URL_HEADER = "**!kawanfw_url_header!**";

    /**
     * Transforms an URL to serialized Base 64 String.
     *
     * @param url
     *            the URL to transform
     * @return a serialized URL in Base64 format
     * @throws IOException
     */
    public String toBase64(URL url) throws IOException {

	ByteArrayOutputStream bos = new ByteArrayOutputStream();

	ObjectOutputStream oos = null;
	try {
	    oos = new ObjectOutputStream(bos);
	    oos.writeObject(url);
	    oos.flush();

	    byte[] byteArray = bos.toByteArray();
	    String base64 = URL_HEADER + Base64.byteArrayToBase64(byteArray);
	    return base64;
	} finally {
	    if (oos != null) {
		oos.close();
	    }
	}
    }

    /**
     * Transforms a serialized Base 64 String to an URL .
     *
     * @param string
     *            a serialized URL in Base64 format
     * @return the rebuilt URL
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public URL fromBase64(final String string) throws IOException, ClassNotFoundException {

	String stringNew = StringUtils.substringAfter(string, URL_HEADER);

	byte[] byteArray = Base64.base64ToByteArray(stringNew);
	ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);

	ObjectInputStream ois = new ObjectInputStream(bis);

	URL url = null;
	try {
	    url = (URL) ois.readObject();
	    return url;
	} finally {
	    if (ois != null) {
		ois.close();
	    }
	}
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy