Page MenuHomec4science

URLParserDecrypt.java
No OneTemporary

File Metadata

Created
Tue, Jan 7, 00:10

URLParserDecrypt.java

package io.serializer.url;
import io.serializer.ParserNamespace;
import java.util.ArrayList;
import java.util.LinkedList;
import utils.StringUtils;
import crypto.elgamal.fragment.FragmentEncoded;
import framework.request.RequestDecryption;
import framework.test.TestResultEncoded;
public class URLParserDecrypt extends URLParser {
public static String requestToString(RequestDecryption request) {
StringBuilder sb = new StringBuilder();
sb.append(ParserNamespace.CNAME_PATIENT).append(QUERY_ASSIGN);
sb.append(Integer.toString(request.getPatientId()));
sb.append(QUERY_AND);
sb.append(ParserNamespace.CNAME_TIMESTAMP).append(QUERY_ASSIGN);
sb.append(Long.toString(request.getTimestamp()));
sb.append(QUERY_AND);
sb.append(ParserNamespace.CNAME_RQT_DECRYPT_VAL).append(QUERY_ASSIGN);
sb.append(convertRequestToBase64(request));
return sb.toString();
}
public static RequestDecryption stringToRequest(String requestString) {
String[] parts = requestString.split(QUERY_AND);
int patientId = extractPatientId(parts[0]);
long timestamp = extractTimestamp(parts[1]);
String rqtVal = extractRequestVal(parts[2]);
RequestDecryption rqt = convertBase64ToRequest(rqtVal, patientId, timestamp);
return rqt;
}
private static String extractRequestVal(String val) {
return extractVal(val, ParserNamespace.CNAME_RQT_DECRYPT_VAL);
}
private static long extractTimestamp(String val) {
return Long.parseLong(extractVal(val, ParserNamespace.CNAME_TIMESTAMP));
}
private static String convertRequestToBase64(RequestDecryption rqt) {
String results = encodedResultsToString(rqt.getEncodedResults());
String hlas = encodedHLAsToString(rqt.getEncodedHLAs());
return results + SEP_EXT + hlas;
}
private static String encodedHLAsToString(ArrayList<LinkedList<FragmentEncoded>> encodedHLAs) {
if (encodedHLAs == null) {
return "";
}
LinkedList<String> hlas = new LinkedList<>();
hlas.add(hLAListToString(encodedHLAs.get(0)));
hlas.add(hLAListToString(encodedHLAs.get(1)));
hlas.add(hLAListToString(encodedHLAs.get(2)));
return StringUtils.join(SEP_EXT, hlas);
}
private static String hLAListToString(LinkedList<FragmentEncoded> hlaList) {
if (hlaList == null) {
return "";
}
LinkedList<String> hlaListString = new LinkedList<>();
for (FragmentEncoded frag : hlaList) {
hlaListString.add(frag.encodeBase64(SEP_INT));
}
return StringUtils.join(SEP_INT, hlaListString);
}
private static String encodedResultsToString(LinkedList<TestResultEncoded> testResults) {
if (testResults == null) {
return "";
}
LinkedList<String> resultsString = new LinkedList<>();
for (TestResultEncoded test : testResults) {
resultsString.add(test.getGenomic().encodeBase64(SEP_INT));
if (test.getClinical() != null) {
resultsString.add(test.getClinical().encodeBase64(SEP_INT));
} else {
resultsString.add("");
resultsString.add("");
}
}
return StringUtils.join(SEP_INT, resultsString);
}
private static RequestDecryption convertBase64ToRequest(String rqtVal, int patientId, long timestamp) {
String[] rqtParts = rqtVal.split(SEP_EXT, -1);
String[] resultsParts = rqtParts[0].split(SEP_INT);
String[] hlaAParts = rqtParts[1].split(SEP_INT);
String[] hlaBParts = rqtParts[2].split(SEP_INT);
String[] hlaCParts = rqtParts[3].split(SEP_INT);
LinkedList<TestResultEncoded> results = parseResultsString(resultsParts);
ArrayList<LinkedList<FragmentEncoded>> hlas = new ArrayList<>(3);
hlas.add(parseHLAString(hlaAParts));
hlas.add(parseHLAString(hlaBParts));
hlas.add(parseHLAString(hlaCParts));
RequestDecryption rqt = new RequestDecryption(patientId, results, hlas);
rqt.setTimestamp(timestamp);
return rqt;
}
private static LinkedList<FragmentEncoded> parseHLAString(String[] parts) {
LinkedList<FragmentEncoded> hlas = new LinkedList<>();
for (int i = 0; i < parts.length; i+=2) {
hlas.add(new FragmentEncoded(parts[i], parts[i+1]));
}
return hlas;
}
private static LinkedList<TestResultEncoded> parseResultsString(String[] parts) {
LinkedList<TestResultEncoded> results = new LinkedList<>();
for (int i = 0; i < parts.length; i+=4) {
FragmentEncoded genomic = new FragmentEncoded(parts[i], parts[i+1]);
FragmentEncoded clinical = new FragmentEncoded(parts[i+2], parts[i+3]);
results.add(new TestResultEncoded(genomic, clinical));
}
return results;
}
}

Event Timeline