Commit cb5cdd73 authored by Grégory Mantelet's avatar Grégory Mantelet
Browse files

[UWS] Make the default backup mechanism independent from the library `cos`.

_This latter will soon be removed from UWSLib._
parent e447a487
Loading
Loading
Loading
Loading
+63 −29
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ import java.util.NoSuchElementException;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.bind.DatatypeConverter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -47,9 +49,6 @@ import org.json.JSONTokener;
import org.json.JSONWriter;
import org.json.Json4Uws;

import com.oreilly.servlet.Base64Decoder;
import com.oreilly.servlet.Base64Encoder;

import uws.ISO8601Format;
import uws.UWSException;
import uws.UWSToolBox;
@@ -87,7 +86,7 @@ import uws.service.request.UploadFile;
 * <p>Another positive value will be considered as the frequency (in milliseconds) of the automatic backup (= {@link #saveAll()}).</p>
 *
 * @author Gr&eacute;gory Mantelet (CDS;ARI)
 * @version 4.3 (05/2018)
 * @version 4.4 (08/2018)
 */
public class DefaultUWSBackupManager implements UWSBackupManager {

@@ -619,19 +618,21 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
			oOutput = new ObjectOutputStream(bArray);
			oOutput.writeObject(jobInfo);
			oOutput.flush();
			return Base64Encoder.encode(bArray.toByteArray());
			return toBase64(bArray.toByteArray());
		}catch(IOException ioe){
			throw new UWSException(UWSException.INTERNAL_SERVER_ERROR, ioe, "Unexpected error while serializing the given JobInfo!");
		}finally{
			if (oOutput != null){
				try{
					oOutput.close();
				}catch(IOException ioe){}
				}catch(IOException ioe){
				}
			}
			if (bArray != null){
				try{
					bArray.close();
				}catch(IOException ioe){}
				}catch(IOException ioe){
				}
			}
		}
	}
@@ -656,7 +657,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
	protected JobInfo restoreJobInfo(final Object jsonValue) throws UWSException, JSONException{
		ObjectInputStream oInput = null;
		try{
			byte[] bArray = Base64Decoder.decodeToBytes((String)jsonValue);
			byte[] bArray = fromBase64((String)jsonValue);
			oInput = new ObjectInputStream(new ByteArrayInputStream(bArray));
			return (JobInfo)oInput.readObject();
		}catch(Exception ex){
@@ -665,10 +666,41 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
			if (oInput != null){
				try{
					oInput.close();
				}catch(IOException ioe){}
				}catch(IOException ioe){
				}
			}
		}
	}

	/**
	 * Encode the given bytes into a Base-64 string.
	 *
	 * @param bytes	Bytes to encode.
	 *
	 * @return	Base-64 encoded string.
	 *
	 * @since 4.4
	 *
	 * @see #fromBase64(String)
	 */
	protected final String toBase64(final byte[] bytes){
		return DatatypeConverter.printBase64Binary(bytes);
	}

	/**
	 * Decode the given Base-64 string into a bytes array.
	 *
	 * @param base64Str	Base-64 string to decode.
	 *
	 * @return	Decoded bytes.
	 *
	 * @since 4.4
	 *
	 * @see #toBase64(byte[])
	 */
	protected final byte[] fromBase64(final String base64Str){
		return DatatypeConverter.parseBase64Binary(base64Str);
	}

	/**
	 * Get the JSON representation of the given {@link UploadFile}.
@@ -1032,7 +1064,8 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
			UploadFile upl;
			try{
				for(int i = 0; i < uploads.length(); i++){
					upl = getUploadFile(uploads.getJSONObject(i));;
					upl = getUploadFile(uploads.getJSONObject(i));
					;
					if (upl != null)
						params.put(upl.paramName, upl);
				}
@@ -1150,7 +1183,8 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
			try{
				if (obj.has("length"))
					upl.length = Long.parseLong(obj.getString("length"));
			}catch(NumberFormatException ex){}
			}catch(NumberFormatException ex){
			}
			return upl;
		}catch(JSONException je){
			getLogger().logUWS(LogLevel.ERROR, obj, "RESTORATION", "Incorrect JSON format for the serialization of an uploaded file!", je);