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
|
/*-
* ============LICENSE_START=======================================================
* org.openecomp.aai
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.aai.introspection;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
import org.openecomp.aai.db.AAIProperties;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.logging.AAILogger;
import org.openecomp.aai.logging.LogLineBuilder;
import org.openecomp.aai.rest.MediaType;
import org.openecomp.aai.workarounds.NamingExceptions;
import com.google.common.base.CaseFormat;
public class PojoLoader extends Loader {
protected JAXBContext context;
private Unmarshaller unmarshaller = null;
private String POJO_CLASSPATH = "org.openecomp.aai.domain.yang";
private final String className = PojoLoader.class.getSimpleName();
private AAILogger aaiLogger = new AAILogger(PojoLoader.class.getName());
protected PojoLoader(Version version, LogLineBuilder llBuilder) {
super(version, ModelType.POJO, llBuilder);
try {
if (!version.equals(AAIProperties.LATEST)) {
POJO_CLASSPATH += "." + version;
}
context = JAXBContext.newInstance(POJO_CLASSPATH);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Introspector introspectorFromName(String name) {
Introspector result = null;
Object temp = this.objectFromName(name);
if (temp != null) {
result = IntrospectorFactory.newInstance(ModelType.POJO, temp, llBuilder);
}
return result;
}
@Override
public Object objectFromName(String name) {
String upperCamel = "";
NamingExceptions exceptions = NamingExceptions.getInstance();
name = exceptions.getObjectName(name);
//Contains any uppercase, then assume it's upper camel
if (name.matches(".*[A-Z].*")) {
upperCamel = name;
} else {
upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name);
}
Object result = null;
try {
if (!upperCamel.contains(POJO_CLASSPATH)) {
upperCamel = POJO_CLASSPATH + "." + upperCamel;
}
result = Class.forName(upperCamel).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
//entity does not exist
}
return result;
}
@Override
protected void process(Version version) {
}
@Override
public Introspector unmarshal(String type, String json, MediaType mediaType) {
Introspector result = null;
Object obj = null;
Object clazz = this.objectFromName(type);
try {
unmarshaller = context.createUnmarshaller();
if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
}
obj = unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue();
result = IntrospectorFactory.newInstance(ModelType.POJO, obj, llBuilder);
} catch (JAXBException e) {
AAIException ex = new AAIException("AAI_4007", e);
aaiLogger.error(ex.getErrorObject(), llBuilder.build(className, "could not unmarshal"), e);
}
return result;
}
}
|