forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
313 lines (264 loc) · 10.6 KB
/
Utils.java
File metadata and controls
313 lines (264 loc) · 10.6 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (c) 2016. Amazon.com, Inc. or its affiliates. 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.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.
*/
package com.amazonaws.codegen.internal;
import com.amazonaws.codegen.model.config.customization.CustomizationConfig;
import com.amazonaws.codegen.model.intermediate.IntermediateModel;
import com.amazonaws.codegen.model.intermediate.Metadata;
import com.amazonaws.codegen.model.intermediate.ShapeMarshaller;
import com.amazonaws.codegen.model.intermediate.ShapeModel;
import com.amazonaws.codegen.model.service.Input;
import com.amazonaws.codegen.model.service.Operation;
import com.amazonaws.codegen.model.service.ServiceMetadata;
import com.amazonaws.codegen.model.service.ServiceModel;
import com.amazonaws.codegen.model.service.Shape;
import com.amazonaws.codegen.model.service.XmlNamespace;
import com.amazonaws.util.StringUtils;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.stream.Stream;
import static com.amazonaws.codegen.internal.Constants.LOGGER;
public class Utils {
public static boolean isScalar(Shape shape) {
// enums are treated as scalars in C2j.
return !(isListShape(shape) || isStructure(shape) || isMapShape(shape));
}
public static boolean isStructure(Shape shape) {
return shape.getType().equals("structure");
}
public static boolean isListShape(Shape shape) {
return shape.getType().equals("list");
}
public static boolean isMapShape(Shape shape) {
return shape.getType().equals("map");
}
public static boolean isEnumShape(Shape shape) {
return shape.getEnumValues() != null;
}
public static boolean isExceptionShape(Shape shape) {
return shape.isException() || shape.isFault();
}
public static String getAsyncInterfaceName(String interfaceNamePrefix) {
return interfaceNamePrefix + Constants.ASYNC_SUFFIX;
}
public static String getClientName(String clientNamePrefix) {
return clientNamePrefix + Constants.CLIENT_NAME_SUFFIX;
}
public static String unCapitialize(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Name cannot be null or empty");
}
return name.length() < 2 ? StringUtils.lowerCase(name) : StringUtils.lowerCase(name.substring(0, 1))
+ name.substring(1);
}
public static Stream<String> sanitize(String toBeSanitized) {
return Arrays.stream(toBeSanitized.split("[.]|\\W")).filter(StringUtils::hasValue);
}
public static String capitialize(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Name cannot be null or empty");
}
return name.length() < 2 ? StringUtils.upperCase(name) : StringUtils.upperCase(name.substring(0, 1))
+ name.substring(1);
}
/**
* * @param serviceModel Service model to get prefix for.
* * @return Prefix to use when writing model files (service and intermediate).
*/
public static String getFileNamePrefix(ServiceModel serviceModel, CustomizationConfig customizationConfig) {
if (customizationConfig.isUseUidAsFilePrefix() && serviceModel.getMetadata().getUid() != null) {
return serviceModel.getMetadata().getUid();
}
return String.format("%s-%s", serviceModel.getMetadata().getEndpointPrefix(), serviceModel.getMetadata().getApiVersion());
}
/**
* Converts a directory to a Java package name.
*
* @param directoryPath Directory to convert.
* @return Package name
*/
public static String directoryToPackage(String directoryPath) {
return directoryPath.replace('/', '.');
}
public static String getDefaultEndpointWithoutHttpProtocol(String endpoint) {
if (endpoint == null) {
return null;
}
if (endpoint.startsWith("http://")) {
return endpoint.substring("http://".length());
}
if (endpoint.startsWith("https://")) {
return endpoint.substring("https://".length());
}
return endpoint;
}
public static File createDirectory(String path) {
if (isNullOrEmpty(path)) {
throw new IllegalArgumentException(
"Invalid path directory. Path directory cannot be null or empty");
}
final File dir = new File(path);
createDirectory(dir);
return dir;
}
public static void createDirectory(File dir) {
if (!(dir.exists())) {
if (!dir.mkdirs()) {
throw new RuntimeException("Not able to create directory. "
+ dir.getAbsolutePath());
}
}
}
public static File createFile(String dir, String fileName) throws IOException {
if (isNullOrEmpty(fileName)) {
throw new IllegalArgumentException(
"Invalid file name. File name cannot be null or empty");
}
createDirectory(dir);
File file = new File(dir, fileName);
if (!(file.exists())) {
if (!(file.createNewFile())) {
throw new RuntimeException("Not able to create file . "
+ file.getAbsolutePath());
}
}
return file;
}
public static boolean isNullOrEmpty(String str) {
return str == null || str.trim().isEmpty();
}
public static void closeQuietly(Closeable closeable) {
if (closeable == null)
return;
try {
closeable.close();
} catch (Exception e) {
LOGGER.debug("Not able to close the stream.");
}
}
/**
* Return an InputStream of the specified resource, failing if it can't be found.
*
* @param clzz
* @param location
* Location of resource
*/
public static InputStream getRequiredResourceAsStream(Class<?> clzz, String location) {
InputStream resourceStream = clzz.getResourceAsStream(location);
if (resourceStream == null) {
// Try with a leading "/"
if (!location.startsWith("/")) {
resourceStream = clzz.getResourceAsStream("/" + location);
}
if (resourceStream == null) {
throw new RuntimeException("Resource file was not found at location " + location);
}
}
return resourceStream;
}
/**
* Retrieve system property by name, failing if it's not found
*
* @param propertyName
* @param errorMsgIfNotFound
* @return Value of property if it exists
*/
public static String getRequiredSystemProperty(String propertyName, String errorMsgIfNotFound) {
String propertyValue = System.getProperty(propertyName);
if (propertyValue == null) {
throw new RuntimeException(errorMsgIfNotFound);
}
return propertyValue;
}
/**
* Retrieve optional system property by name, returning null if not found
*
* @param propertyName
* @return Value of property if it exists, null if it does not
*/
public static String getOptionalSystemProperty(String propertyName) {
return System.getProperty(propertyName);
}
/**
* Throws IllegalArgumentException with the specified error message if the input
* is null, otherwise return the input as is.
*/
public static <T> T assertNotNull(T argument, String msg) {
if (argument == null) throw new IllegalArgumentException(msg);
return argument;
}
/**
* Search for intermediate shape model by its c2j name.
*
* @return ShapeModel
* @throws IllegalArgumentException
* if the specified c2j name is not found in the intermediate model.
*/
public static ShapeModel findShapeModelByC2jName(IntermediateModel intermediateModel, String shapeC2jName)
throws IllegalArgumentException {
ShapeModel shapeModel = findShapeModelByC2jNameIfExists(intermediateModel, shapeC2jName);
if (shapeModel != null) {
return shapeModel;
} else {
throw new IllegalArgumentException(
shapeC2jName + " shape (c2j name) does not exist in the intermediate model.");
}
}
/**
* Search for intermediate shape model by its c2j name.
*
* @return ShapeModel or null if the shape doesn't exist (if it's primitive or container type for example)
*/
public static ShapeModel findShapeModelByC2jNameIfExists(IntermediateModel intermediateModel, String shapeC2jName) {
for (ShapeModel shape : intermediateModel.getShapes().values()) {
if (shape.getC2jName().equals(shapeC2jName)) {
return shape;
}
}
return null;
}
/**
* Create the ShapeMarshaller to the input shape from the specified Operation.
* The input shape in the operation could be empty.
*
* @param service
* @param operation
* @return
*/
public static ShapeMarshaller createInputShapeMarshaller(ServiceMetadata service, Operation operation) {
if (operation == null)
throw new IllegalArgumentException(
"The operation parameter must be specified!");
ShapeMarshaller marshaller = new ShapeMarshaller()
.withAction(operation.getName())
.withVerb(operation.getHttp().getMethod())
.withRequestUri(operation.getHttp().getRequestUri());
Input input = operation.getInput();
if (input != null) {
marshaller.setLocationName(input.getLocationName());
// Pass the xmlNamespace trait from the input reference
XmlNamespace xmlNamespace = input.getXmlNamespace();
if (xmlNamespace != null) {
marshaller.setXmlNameSpaceUri(xmlNamespace.getUri());
}
}
if (!StringUtils.isNullOrEmpty(service.getTargetPrefix()) && Metadata.isNotRestProtocol(service.getProtocol())) {
marshaller.setTarget(service.getTargetPrefix() + "." + operation.getName());
}
return marshaller;
}
}