use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class ClientProxyImpl method handleMultipart.
private List<Attachment> handleMultipart(MultivaluedMap<ParameterType, Parameter> map, OperationResourceInfo ori, Object[] params) {
List<Attachment> atts = new LinkedList<Attachment>();
List<Parameter> fm = getParameters(map, ParameterType.REQUEST_BODY);
for (Parameter p : fm) {
Multipart part = getMultipart(ori, p.getIndex());
if (part != null) {
Object partObject = params[p.getIndex()];
if (partObject != null) {
atts.add(new Attachment(part.value(), part.type(), partObject));
}
}
}
return atts;
}
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<String, List<UserOperation>>();
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<Parameter>();
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");
ParameterType pType = null;
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.get(opTag).add(userOp);
}
}
}
List<UserResource> resources = new LinkedList<UserResource>();
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 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<String, List<UserOperation>>();
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"));
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<Parameter>();
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<UserResource>();
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 SwaggerParseUtilsTest method verifyPetResource.
// CHECKSTYLE:OFF
private void verifyPetResource(UserResource ur) {
assertNotNull(ur);
assertEquals("/", ur.getPath());
assertEquals(8, ur.getOperations().size());
// POST /pet
UserOperation addPet = ur.getOperations().get(0);
assertEquals("addPet", addPet.getName());
assertEquals("/pet", addPet.getPath());
assertEquals("POST", addPet.getVerb());
assertEquals("application/xml,application/json", addPet.getProduces());
assertEquals("application/json,application/xml", addPet.getConsumes());
assertEquals(1, addPet.getParameters().size());
Parameter addPetParam1 = addPet.getParameters().get(0);
assertEquals("body", addPetParam1.getName());
assertEquals(ParameterType.REQUEST_BODY, addPetParam1.getType());
// TODO: check that addPetParam1 refers to Pet model
// TODO: check default response type: JAX-RS Response ?
// PUT /pet
UserOperation putPet = ur.getOperations().get(1);
assertEquals("updatePet", putPet.getName());
assertEquals("/pet", putPet.getPath());
assertEquals("PUT", putPet.getVerb());
assertEquals("application/xml,application/json", addPet.getProduces());
assertEquals("application/json,application/xml", addPet.getConsumes());
assertEquals(1, putPet.getParameters().size());
Parameter putPetParam1 = putPet.getParameters().get(0);
assertEquals("body", putPetParam1.getName());
assertEquals(ParameterType.REQUEST_BODY, putPetParam1.getType());
// TODO: check that putPetParam1 refers to Pet model
// TODO: check default response type: JAX-RS Response ?
// GET /pet/findByStatus
UserOperation findPetsByStatus = ur.getOperations().get(2);
assertEquals("findPetsByStatus", findPetsByStatus.getName());
assertEquals("/pet/findByStatus", findPetsByStatus.getPath());
assertEquals("GET", findPetsByStatus.getVerb());
assertEquals("application/xml,application/json", findPetsByStatus.getProduces());
assertNull(findPetsByStatus.getConsumes());
assertEquals(1, findPetsByStatus.getParameters().size());
Parameter findPetsByStatusParam1 = findPetsByStatus.getParameters().get(0);
assertEquals("status", findPetsByStatusParam1.getName());
assertEquals(ParameterType.QUERY, findPetsByStatusParam1.getType());
// TODO: check that findPetsByStatusParam1 refers to List of String
// TODO: check that response refers to List of Pets
// GET /pet/findByTag
UserOperation findPetsByTags = ur.getOperations().get(3);
assertEquals("findPetsByTags", findPetsByTags.getName());
assertEquals("/pet/findByTags", findPetsByTags.getPath());
assertEquals("GET", findPetsByTags.getVerb());
assertEquals("application/xml,application/json", findPetsByTags.getProduces());
assertNull(findPetsByTags.getConsumes());
assertEquals(1, findPetsByTags.getParameters().size());
Parameter findPetsByTagsParam1 = findPetsByTags.getParameters().get(0);
assertEquals("tags", findPetsByTagsParam1.getName());
assertEquals(ParameterType.QUERY, findPetsByTagsParam1.getType());
// TODO: check that findPetsByTagsParam1 refers to List of String
// TODO: check that response refers to List of Pets
// GET /pet/{petId}
UserOperation getPetById = ur.getOperations().get(4);
assertEquals("getPetById", getPetById.getName());
assertEquals("/pet/{petId}", getPetById.getPath());
assertEquals("GET", getPetById.getVerb());
assertEquals("application/xml,application/json", getPetById.getProduces());
assertNull(getPetById.getConsumes());
assertEquals(1, getPetById.getParameters().size());
Parameter getPetByIdParam1 = getPetById.getParameters().get(0);
assertEquals("petId", getPetByIdParam1.getName());
assertEquals(ParameterType.PATH, getPetByIdParam1.getType());
assertEquals(long.class, getPetByIdParam1.getJavaType());
// POST /pet/{petId}
UserOperation updatePetWithForm = ur.getOperations().get(5);
assertEquals("updatePetWithForm", updatePetWithForm.getName());
assertEquals("/pet/{petId}", updatePetWithForm.getPath());
assertEquals("POST", updatePetWithForm.getVerb());
assertEquals("application/xml,application/json", updatePetWithForm.getProduces());
assertEquals("application/x-www-form-urlencoded", updatePetWithForm.getConsumes());
assertEquals(3, updatePetWithForm.getParameters().size());
Parameter updatePetWithFormParam1 = updatePetWithForm.getParameters().get(0);
assertEquals("petId", updatePetWithFormParam1.getName());
assertEquals(ParameterType.PATH, updatePetWithFormParam1.getType());
assertEquals(long.class, updatePetWithFormParam1.getJavaType());
Parameter updatePetWithFormParam2 = updatePetWithForm.getParameters().get(1);
assertEquals("name", updatePetWithFormParam2.getName());
assertEquals(ParameterType.FORM, updatePetWithFormParam2.getType());
assertEquals(String.class, updatePetWithFormParam2.getJavaType());
Parameter updatePetWithFormParam3 = updatePetWithForm.getParameters().get(2);
assertEquals("status", updatePetWithFormParam3.getName());
assertEquals(ParameterType.FORM, updatePetWithFormParam3.getType());
assertEquals(String.class, updatePetWithFormParam3.getJavaType());
// DELETE /pet/{petId}
UserOperation deletePet = ur.getOperations().get(6);
assertEquals("deletePet", deletePet.getName());
assertEquals("/pet/{petId}", deletePet.getPath());
assertEquals("DELETE", deletePet.getVerb());
assertEquals("application/xml,application/json", deletePet.getProduces());
assertNull(deletePet.getConsumes());
assertEquals(2, deletePet.getParameters().size());
Parameter deletePetParam1 = deletePet.getParameters().get(0);
assertEquals("api_key", deletePetParam1.getName());
assertEquals(ParameterType.HEADER, deletePetParam1.getType());
assertEquals(String.class, deletePetParam1.getJavaType());
Parameter deletePetParam2 = deletePet.getParameters().get(1);
assertEquals("petId", deletePetParam2.getName());
assertEquals(ParameterType.PATH, deletePetParam2.getType());
assertEquals(long.class, deletePetParam2.getJavaType());
// POST /pet/{petId}/uploadImage
UserOperation uploadFile = ur.getOperations().get(7);
assertEquals("uploadFile", uploadFile.getName());
assertEquals("/pet/{petId}/uploadImage", uploadFile.getPath());
assertEquals("POST", uploadFile.getVerb());
assertEquals("application/json", uploadFile.getProduces());
assertEquals("multipart/form-data", uploadFile.getConsumes());
assertEquals(3, uploadFile.getParameters().size());
Parameter uploadFileParam1 = uploadFile.getParameters().get(0);
assertEquals("petId", uploadFileParam1.getName());
assertEquals(ParameterType.PATH, uploadFileParam1.getType());
assertEquals(long.class, uploadFileParam1.getJavaType());
Parameter uploadFileParam2 = uploadFile.getParameters().get(1);
assertEquals("additionalMetadata", uploadFileParam2.getName());
assertEquals(ParameterType.FORM, uploadFileParam2.getType());
assertEquals(String.class, uploadFileParam2.getJavaType());
Parameter uploadFileParam3 = uploadFile.getParameters().get(2);
assertEquals("file", uploadFileParam3.getName());
assertEquals(ParameterType.FORM, uploadFileParam3.getType());
assertEquals(InputStream.class, uploadFileParam3.getJavaType());
}
use of org.apache.cxf.jaxrs.model.Parameter in project cxf by apache.
the class SwaggerParseUtilsTest method verifyPetUserResource.
// CHECKSTYLE:ON
// CHECKSTYLE:OFF
private void verifyPetUserResource(UserResource ur) {
assertNotNull(ur);
assertEquals("/", ur.getPath());
assertEquals(8, ur.getOperations().size());
// POST /user
UserOperation createUser = ur.getOperations().get(0);
assertEquals("createUser", createUser.getName());
assertEquals("/user", createUser.getPath());
assertEquals("POST", createUser.getVerb());
assertEquals("application/xml,application/json", createUser.getProduces());
assertNull(createUser.getConsumes());
assertEquals(1, createUser.getParameters().size());
Parameter createUserParam1 = createUser.getParameters().get(0);
assertEquals("body", createUserParam1.getName());
assertEquals(ParameterType.REQUEST_BODY, createUserParam1.getType());
// TODO: check that createUserParam1 refers to User model
// TODO: check default response type: JAX-RS Response ?
// POST /user/createWithArray
UserOperation createWithArray = ur.getOperations().get(1);
assertEquals("createUsersWithArrayInput", createWithArray.getName());
assertEquals("/user/createWithArray", createWithArray.getPath());
assertEquals("POST", createUser.getVerb());
assertEquals("application/xml,application/json", createUser.getProduces());
assertNull(createWithArray.getConsumes());
assertEquals(1, createUser.getParameters().size());
Parameter createWithArrayParam1 = createUser.getParameters().get(0);
assertEquals("body", createWithArrayParam1.getName());
assertEquals(ParameterType.REQUEST_BODY, createWithArrayParam1.getType());
// TODO: check that createUserParam1 refers to an array of User model
// TODO: check default response type: JAX-RS Response ?
// POST /user/createWithList
UserOperation createWithList = ur.getOperations().get(2);
assertEquals("createUsersWithListInput", createWithList.getName());
assertEquals("/user/createWithList", createWithList.getPath());
assertEquals("POST", createWithList.getVerb());
assertEquals("application/xml,application/json", createWithList.getProduces());
assertNull(createWithList.getConsumes());
assertEquals(1, createWithList.getParameters().size());
Parameter createWithListParam1 = createWithList.getParameters().get(0);
assertEquals("body", createWithListParam1.getName());
assertEquals(ParameterType.REQUEST_BODY, createWithListParam1.getType());
// TODO: check that createUserParam1 refers to an array of User model
// TODO: check default response type: JAX-RS Response ?
// GET /user/login
UserOperation loginUser = ur.getOperations().get(3);
assertEquals("loginUser", loginUser.getName());
assertEquals("/user/login", loginUser.getPath());
assertEquals("GET", loginUser.getVerb());
assertEquals("application/xml,application/json", loginUser.getProduces());
assertNull(loginUser.getConsumes());
assertEquals(2, loginUser.getParameters().size());
Parameter loginUserParam1 = loginUser.getParameters().get(0);
assertEquals("username", loginUserParam1.getName());
assertEquals(ParameterType.QUERY, loginUserParam1.getType());
assertEquals(String.class, loginUserParam1.getJavaType());
Parameter loginUserParam2 = loginUser.getParameters().get(1);
assertEquals("password", loginUserParam2.getName());
assertEquals(ParameterType.QUERY, loginUserParam2.getType());
assertEquals(String.class, loginUserParam2.getJavaType());
// TODO: check 200 response type
// GET /user/logout
UserOperation logoutUser = ur.getOperations().get(4);
assertEquals("logoutUser", logoutUser.getName());
assertEquals("/user/logout", logoutUser.getPath());
assertEquals("GET", loginUser.getVerb());
assertEquals("application/xml,application/json", logoutUser.getProduces());
assertNull(logoutUser.getConsumes());
assertEquals(0, logoutUser.getParameters().size());
// TODO: check 200 response type
// GET /user/{username}
UserOperation getUserByName = ur.getOperations().get(5);
assertEquals("getUserByName", getUserByName.getName());
assertEquals("/user/{username}", getUserByName.getPath());
assertEquals("GET", getUserByName.getVerb());
assertEquals("application/xml,application/json", getUserByName.getProduces());
assertNull(getUserByName.getConsumes());
assertEquals(1, getUserByName.getParameters().size());
Parameter getUserByNameParam1 = getUserByName.getParameters().get(0);
assertEquals("username", getUserByNameParam1.getName());
assertEquals(ParameterType.PATH, getUserByNameParam1.getType());
assertEquals(String.class, getUserByNameParam1.getJavaType());
// TODO: check 200 response type
// PUT /user/{username}
UserOperation updateUser = ur.getOperations().get(6);
assertEquals("updateUser", updateUser.getName());
assertEquals("/user/{username}", updateUser.getPath());
assertEquals("PUT", updateUser.getVerb());
assertEquals("application/xml,application/json", updateUser.getProduces());
assertNull(updateUser.getConsumes());
assertEquals(2, updateUser.getParameters().size());
Parameter updateUserParam1 = updateUser.getParameters().get(0);
assertEquals("username", updateUserParam1.getName());
assertEquals(ParameterType.PATH, updateUserParam1.getType());
assertEquals(String.class, updateUserParam1.getJavaType());
Parameter updateUserParam2 = updateUser.getParameters().get(1);
assertEquals("body", updateUserParam2.getName());
assertEquals(ParameterType.REQUEST_BODY, updateUserParam2.getType());
// TODO: check that createUserParam1 refers to an array of User model
// TODO: check 200 response type
// DELETE /user/{username}
UserOperation deleteUser = ur.getOperations().get(7);
assertEquals("deleteUser", deleteUser.getName());
assertEquals("/user/{username}", deleteUser.getPath());
assertEquals("DELETE", deleteUser.getVerb());
assertEquals("application/xml,application/json", deleteUser.getProduces());
assertNull(deleteUser.getConsumes());
assertEquals(1, deleteUser.getParameters().size());
Parameter deleteUserParam1 = deleteUser.getParameters().get(0);
assertEquals("username", deleteUserParam1.getName());
assertEquals(ParameterType.PATH, deleteUserParam1.getType());
assertEquals(String.class, deleteUserParam1.getJavaType());
}
Aggregations