use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class SwaggerParseUtils method getUserApplicationFromJson.
public static UserApplication getUserApplicationFromJson(String json, ParseConfiguration cfg) {
JsonMapObjectReaderWriter reader = new JsonMapObjectReaderWriter();
Map<String, Object> map = reader.fromJson(json);
UserApplication app = new UserApplication();
String relativePath = (String) map.get("basePath");
app.setBasePath(StringUtils.isEmpty(relativePath) ? "/" : relativePath);
Map<String, List<UserOperation>> userOpsMap = new LinkedHashMap<>();
Set<String> tags = new HashSet<>();
List<Map<String, Object>> tagsProp = CastUtils.cast((List<?>) map.get("tags"));
if (tagsProp != null) {
for (Map<String, Object> tagProp : tagsProp) {
tags.add((String) tagProp.get("name"));
}
} else {
tags.add("");
}
for (String tag : tags) {
userOpsMap.put(tag, new LinkedList<UserOperation>());
}
Map<String, Map<String, Object>> paths = CastUtils.cast((Map<?, ?>) map.get("paths"));
for (Map.Entry<String, Map<String, Object>> pathEntry : paths.entrySet()) {
String operPath = pathEntry.getKey();
Map<String, Object> operations = pathEntry.getValue();
for (Map.Entry<String, Object> operEntry : operations.entrySet()) {
UserOperation userOp = new UserOperation();
userOp.setVerb(operEntry.getKey().toUpperCase());
Map<String, Object> oper = CastUtils.cast((Map<?, ?>) operEntry.getValue());
// operations but Swagger may still include it.
if (oper != null) {
userOp.setPath(operPath);
userOp.setName((String) oper.get("operationId"));
List<String> opProduces = CastUtils.cast((List<?>) oper.get("produces"));
userOp.setProduces(listToString(opProduces));
List<String> opConsumes = CastUtils.cast((List<?>) oper.get("consumes"));
userOp.setConsumes(listToString(opConsumes));
List<Parameter> userOpParams = new LinkedList<>();
List<Map<String, Object>> params = CastUtils.cast((List<?>) oper.get("parameters"));
for (Map<String, Object> param : params) {
String name = (String) param.get("name");
// "query", "header", "path", "formData" or "body"
String paramType = (String) param.get("in");
ParameterType pType = "body".equals(paramType) ? ParameterType.REQUEST_BODY : "formData".equals(paramType) ? ParameterType.FORM : ParameterType.valueOf(paramType.toUpperCase());
Parameter userParam = new Parameter(pType, name);
setJavaType(userParam, (String) param.get("type"));
userOpParams.add(userParam);
}
if (!userOpParams.isEmpty()) {
userOp.setParameters(userOpParams);
}
List<String> opTags = CastUtils.cast((List<?>) oper.get("tags"));
if (opTags == null) {
opTags = Collections.singletonList("");
}
for (String opTag : opTags) {
userOpsMap.get(opTag).add(userOp);
}
}
}
}
List<UserResource> resources = new LinkedList<>();
for (Map.Entry<String, List<UserOperation>> entry : userOpsMap.entrySet()) {
UserResource ur = new UserResource();
ur.setPath("/");
ur.setOperations(entry.getValue());
ur.setName(entry.getKey());
resources.add(ur);
}
app.setResources(resources);
return app;
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class OpenApiParseUtils method getUserApplicationFromJson.
public static UserApplication getUserApplicationFromJson(String json, ParseConfiguration cfg) {
JsonMapObjectReaderWriter reader = new JsonMapObjectReaderWriter();
Map<String, Object> map = reader.fromJson(json);
UserApplication app = new UserApplication();
app.setBasePath("/");
List<Map<String, Object>> servers = CastUtils.cast((List<?>) map.get("servers"));
if (servers != null && !servers.isEmpty()) {
final String url = (String) servers.get(0).get("url");
if (url != null) {
app.setBasePath(url);
}
}
Map<String, List<UserOperation>> userOpsMap = new LinkedHashMap<>();
Set<String> tags = new HashSet<>();
List<Map<String, Object>> tagsProp = CastUtils.cast((List<?>) map.get("tags"));
if (tagsProp != null) {
for (Map<String, Object> tagProp : tagsProp) {
tags.add((String) tagProp.get("name"));
}
} else {
tags.add("");
}
for (String tag : tags) {
userOpsMap.put(tag, new LinkedList<UserOperation>());
}
Map<String, Map<String, Object>> paths = CastUtils.cast((Map<?, ?>) map.get("paths"));
for (Map.Entry<String, Map<String, Object>> pathEntry : paths.entrySet()) {
String operPath = pathEntry.getKey();
Map<String, Object> operations = pathEntry.getValue();
for (Map.Entry<String, Object> operEntry : operations.entrySet()) {
UserOperation userOp = new UserOperation();
userOp.setVerb(operEntry.getKey().toUpperCase());
Map<String, Object> oper = CastUtils.cast((Map<?, ?>) operEntry.getValue());
userOp.setPath(operPath);
userOp.setName((String) oper.get("operationId"));
Map<String, Object> responses = CastUtils.cast((Map<?, ?>) oper.get("responses"));
if (responses != null) {
userOp.setProduces(listToString(responses.entrySet().stream().map(entry -> CastUtils.cast((Map<?, ?>) entry.getValue())).map(value -> CastUtils.cast((Map<?, ?>) value.get("content"))).filter(Objects::nonNull).flatMap(content -> content.keySet().stream().map(type -> (String) type)).collect(Collectors.toList())));
}
Map<String, Object> payloads = CastUtils.cast((Map<?, ?>) oper.get("requestBody"));
if (payloads != null) {
userOp.setConsumes(listToString(payloads.entrySet().stream().map(entry -> CastUtils.cast((Map<?, ?>) entry.getValue())).map(value -> CastUtils.cast((Map<?, ?>) value.get("content"))).filter(Objects::nonNull).flatMap(content -> content.keySet().stream().map(type -> (String) type)).collect(Collectors.toList())));
}
List<Parameter> userOpParams = new LinkedList<>();
List<Map<String, Object>> params = CastUtils.cast((List<?>) oper.get("parameters"));
if (params != null) {
for (Map<String, Object> param : params) {
String name = (String) param.get("name");
// "query", "header", "path" or "cookie".
String paramType = (String) param.get("in");
final ParameterType pType;
if ("query".equals(paramType)) {
pType = ParameterType.QUERY;
} else if ("header".equals(paramType)) {
pType = ParameterType.HEADER;
} else if ("path".equals(paramType)) {
pType = ParameterType.PATH;
} else if ("cookie".equals(paramType)) {
pType = ParameterType.COOKIE;
} else {
pType = ParameterType.REQUEST_BODY;
}
Parameter userParam = new Parameter(pType, name);
setJavaType(userParam, (String) param.get("type"));
userOpParams.add(userParam);
}
}
if (!userOpParams.isEmpty()) {
userOp.setParameters(userOpParams);
}
List<String> opTags = CastUtils.cast((List<?>) oper.get("tags"));
if (opTags == null) {
opTags = Collections.singletonList("");
}
for (String opTag : opTags) {
userOpsMap.putIfAbsent(opTag, new LinkedList<UserOperation>());
userOpsMap.get(opTag).add(userOp);
}
}
}
List<UserResource> resources = new LinkedList<>();
for (Map.Entry<String, List<UserOperation>> entry : userOpsMap.entrySet()) {
if (!entry.getValue().isEmpty()) {
UserResource ur = new UserResource();
ur.setPath("/");
ur.setOperations(entry.getValue());
ur.setName(entry.getKey());
resources.add(ur);
}
}
app.setResources(resources);
return app;
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class MicroProfileClientProxyImpl method createClientHeaderParameter.
private Parameter createClientHeaderParameter(ClientHeaderParam anno, Class<?> clientIntf) {
Parameter p = new Parameter(ParameterType.HEADER, anno.name());
String[] values = anno.value();
String headerValue;
if (values[0] != null && values[0].length() > 2 && values[0].startsWith("{") && values[0].endsWith("}")) {
try {
headerValue = invokeBestFitComputeMethod(clientIntf, anno);
} catch (Throwable t) {
if (anno.required()) {
throwException(t);
}
return null;
}
} else {
headerValue = HttpUtils.getHeaderString(Arrays.asList(values));
}
p.setDefaultValue(headerValue);
return p;
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class AbstractSwagger2ServiceDescriptionTest method doTestApiListingIsProperlyReturnedJSON.
protected static void doTestApiListingIsProperlyReturnedJSON(final WebClient client, XForwarded useXForwarded) throws Exception {
if (useXForwarded == XForwarded.ONE_HOST) {
client.header("USE_XFORWARDED", true);
} else if (useXForwarded == XForwarded.MANY_HOSTS) {
client.header("USE_XFORWARDED_MANY_HOSTS", true);
}
try {
String swaggerJson = client.get(String.class);
UserApplication ap = SwaggerParseUtils.getUserApplicationFromJson(swaggerJson);
assertNotNull(ap);
assertEquals(useXForwarded.isSet() ? "/reverse" : "/", ap.getBasePath());
List<UserResource> urs = ap.getResources();
assertNotNull(urs);
assertEquals(1, urs.size());
UserResource r = urs.get(0);
String basePath = "";
if (!"/".equals(r.getPath())) {
basePath = r.getPath();
}
Map<String, UserOperation> map = r.getOperationsAsMap();
assertEquals(3, map.size());
UserOperation getBooksOp = map.get("getBooks");
assertEquals(HttpMethod.GET, getBooksOp.getVerb());
assertEquals("/bookstore", basePath + getBooksOp.getPath());
assertEquals(MediaType.APPLICATION_JSON, getBooksOp.getProduces());
List<Parameter> getBooksOpParams = getBooksOp.getParameters();
assertEquals(1, getBooksOpParams.size());
assertEquals(ParameterType.QUERY, getBooksOpParams.get(0).getType());
UserOperation getBookOp = map.get("getBook");
assertEquals(HttpMethod.GET, getBookOp.getVerb());
assertEquals("/bookstore/{id}", basePath + getBookOp.getPath());
assertEquals(MediaType.APPLICATION_JSON, getBookOp.getProduces());
List<Parameter> getBookOpParams = getBookOp.getParameters();
assertEquals(1, getBookOpParams.size());
assertEquals(ParameterType.PATH, getBookOpParams.get(0).getType());
UserOperation deleteOp = map.get("delete");
assertEquals(HttpMethod.DELETE, deleteOp.getVerb());
assertEquals("/bookstore/{id}", basePath + deleteOp.getPath());
List<Parameter> delOpParams = deleteOp.getParameters();
assertEquals(1, delOpParams.size());
assertEquals(ParameterType.PATH, delOpParams.get(0).getType());
assertThat(swaggerJson, CoreMatchers.containsString(CONTACT));
assertThat(swaggerJson, CoreMatchers.containsString(TITLE));
assertThat(swaggerJson, CoreMatchers.containsString(DESCRIPTION));
assertThat(swaggerJson, CoreMatchers.containsString(LICENSE));
assertThat(swaggerJson, CoreMatchers.containsString(LICENSE_URL));
assertThat(swaggerJson, CoreMatchers.containsString(SECURITY_DEFINITION_NAME));
} finally {
client.close();
}
}
Aggregations