summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/dynamicContent/api/DynamicContentCoder.java
blob: 34406e06c9cb7b7b6a0335060f910d8039084d14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package org.uic.barcode.dynamicContent.api;

import java.util.Date;
import java.util.List;

import org.uic.barcode.dynamicContent.fdc1.GeoCoordinateType;
import org.uic.barcode.dynamicContent.fdc1.SequenceOfExtension;
import org.uic.barcode.dynamicContent.fdc1.TimeStamp;
import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1;
import org.uic.barcode.ticket.EncodingFormatException;
import org.uic.barcode.asn1.uper.UperEncoder;
import org.uic.barcode.dynamicContent.fdc1.ExtensionData;
import org.uic.barcode.dynamicContent.fdc1.GeoCoordinateSystemType;
import org.uic.barcode.dynamicContent.fdc1.GeoUnitType;
import org.uic.barcode.dynamicContent.fdc1.HemisphereLatitudeType;
import org.uic.barcode.dynamicContent.fdc1.HemisphereLongitudeType;
import org.uic.barcode.ticket.api.impl.SimpleExtension;
import org.uic.barcode.ticket.api.impl.SimpleGeoCoordinate;
import org.uic.barcode.ticket.api.spec.IExtension;
import org.uic.barcode.ticket.api.spec.IGeoCoordinate;
import org.uic.barcode.ticket.api.spec.IGeoCoordinateSystemType;
import org.uic.barcode.ticket.api.spec.IGeoUnitType;
import org.uic.barcode.ticket.api.spec.IHemisphereLatitudeType;
import org.uic.barcode.ticket.api.spec.IHemisphereLongitudeType;
import org.uic.barcode.ticket.api.utils.UicEncoderUtils;

public class DynamicContentCoder {
	
	public static String dynamicContentDataFDC1 = "FDC1";
	
	public static byte[] encode(IUicDynamicContent content, String format) throws EncodingFormatException {
		
		if (format != null && !format.equals(dynamicContentDataFDC1)) {
			throw new EncodingFormatException("Format of dynamic content not supported!");
		}
				
		UicDynamicContentDataFDC1 asn = new UicDynamicContentDataFDC1();
		
		asn.setAppId(content.getAppId());
		
		if (content.getChallengeString() != null && content.getChallengeString().length() > 0) {
			asn.setChallengeString(content.getChallengeString());
		}

		asn.setDynamicContentExtension(getAsnExtension(content.getExtension()));
		
		asn.setGeoCoordinate(getAsnGeoCoordinate(content.getGeoCoordinate()));
		
		asn.setTimeStamp(getAsnTimeStamp(content.getTimeStamp()));
		
		asn.setExtensions(getAsnContentExtensions(asn, content.getDynamicContentResponseList()));
		
		return UperEncoder.encode(asn);
		
	}
	
	

	private static SequenceOfExtension getAsnContentExtensions(UicDynamicContentDataFDC1 asn, List<IExtension> dynamicContentResponseList) throws EncodingFormatException {
		if (dynamicContentResponseList != null && !dynamicContentResponseList.isEmpty()){
			
			SequenceOfExtension asnList = asn.getExtensions();
			if (asnList == null) asnList = new SequenceOfExtension();
			for (IExtension extension : dynamicContentResponseList){
				ExtensionData asnExtension = getAsnExtension(extension);
				if (asnExtension!= null) {
					asnList.add(asnExtension);
				}
			}
			if (!asnList.isEmpty()){
				return asnList;
			}
		}
	
		return null;
	}

	private static TimeStamp getAsnTimeStamp(Date date) {
		
		if (date == null) return null;
		
		TimeStamp asnTimeStamp = new TimeStamp();
		asnTimeStamp.setDateTime(date);
		
		return asnTimeStamp;
		
	}

	private static GeoCoordinateType getAsnGeoCoordinate(IGeoCoordinate point) {
			
			if (point == null) return null;
			
			GeoCoordinateType asnPoint = new GeoCoordinateType();
			
			asnPoint.setLatitude(point.getLatitude());  
			asnPoint.setLongitude(point.getLongitude());
			
			if (point.getUnit() != IGeoUnitType.milliDegree && point.getUnit() != null){
				asnPoint.setGeoUnit(GeoUnitType.valueOf(point.getUnit().name()));
			}
			
			if (point.getAccuracy() != null) {
				asnPoint.setAccuracy(GeoUnitType.valueOf(point.getAccuracy().name()));
			}
			
			if (point.getHemisphereLatitude() != IHemisphereLatitudeType.east && point.getHemisphereLatitude() != null) {
				asnPoint.setHemisphereLatitude(HemisphereLatitudeType.valueOf(point.getHemisphereLatitude().name()));
			}
			
			if (point.getHemisphereLongitude() != IHemisphereLongitudeType.north && point.getHemisphereLongitude() != null) {
				asnPoint.setHemisphereLongitude(HemisphereLongitudeType.valueOf(point.getHemisphereLongitude().name()));
			}		
			
			if (point.getSystem() != IGeoCoordinateSystemType.wgs84 && point.getSystem() != null){
				asnPoint.setCoordinateSystem(GeoCoordinateSystemType.valueOf(point.getSystem().name()));
			}


			return asnPoint;
	}

	private static ExtensionData getAsnExtension(IExtension extension) throws EncodingFormatException {
		if (extension==null) return null;
		
		if (extension.getBinarydata() == null || extension.getBinarydata().length == 0) {
			throw new EncodingFormatException("Extension does not include data");
		}

		if (extension.getId() == null || extension.getId().length() == 0) {
			throw new EncodingFormatException("Extension does not include id");
		}
		
		ExtensionData asnExtension = new ExtensionData();
		
		asnExtension.setExtensionData(extension.getBinarydata());
		asnExtension.setExtensionId(UicEncoderUtils.getIA5(extension.getId()));

		return asnExtension;
	}

	public static IUicDynamicContent decode(byte[] bytes) {	
		
		UicDynamicContentDataFDC1 asn = UperEncoder.decode(bytes, UicDynamicContentDataFDC1.class);
		
		IUicDynamicContent content = new SimpleUicDynamicContent();
		
		content.setAppId(asn.getAppId());
		
		content.setChallengeString(asn.getChallengeString());

		content.setExtension(getExtension(asn.getDynamicContentExtension()));
		
		if (asn.getExtensions() != null && !asn.getExtensions().isEmpty()) {
			for (ExtensionData e : asn.getExtensions())	{	
				content.addDynamicContentResponse(getExtension(e));
			}
		}
		
		content.setGeoCoordinate(getGeoCoordinate(asn.getGeoCoordinate()));
		
		content.setTimeStamp(asn.getTimeStamp().getTimeAsDate());
		

		return content;

	}

	private static IGeoCoordinate getGeoCoordinate(GeoCoordinateType asnCoordinate) {
		
		IGeoCoordinate g = new SimpleGeoCoordinate();
		
		g.setLatitude(asnCoordinate.getLatitude());
		g.setLongitude(asnCoordinate.getLongitude());
		
		if (asnCoordinate.getCoordinateSystem() != null) {
			g.setSystem(IGeoCoordinateSystemType.valueOf(asnCoordinate.getCoordinateSystem().name()));
		}
		
		if (asnCoordinate.getAccuracy() != null) {
			g.setAccuracy(IGeoUnitType.valueOf(asnCoordinate.getAccuracy().name()));
		}		
		
		if (asnCoordinate.getGeoUnit() != null) {
			g.setUnit(IGeoUnitType.valueOf(asnCoordinate.getGeoUnit().name()));
		}
		
		if (asnCoordinate.getHemisphereLatitude() != null) {
			g.setHemisphereLatitude(IHemisphereLatitudeType.valueOf(asnCoordinate.getHemisphereLatitude().name()));
		}

		if (asnCoordinate.getHemisphereLongitude() != null) {
			g.setHemisphereLongitude(IHemisphereLongitudeType.valueOf(asnCoordinate.getHemisphereLongitude().name()));
		}	
		
		
		return g;
	}



	private static IExtension getExtension(ExtensionData asnExtension) {
		
		if (asnExtension == null) return null;
		
		SimpleExtension e = new SimpleExtension();
		e.setBinarydata(asnExtension.getExtensionData());
		e.setId(asnExtension.getExtensionId());
		
		return e;
	}
	
	
}