NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;

import org.jvnet.ws.wadl.Application;
import org.jvnet.ws.wadl.Doc;
import org.jvnet.ws.wadl.Param;
import org.jvnet.ws.wadl.ParamStyle;
import org.jvnet.ws.wadl.Representation;
import org.jvnet.ws.wadl.Request;
import org.jvnet.ws.wadl.Resource;
import org.jvnet.ws.wadl.Resources;
import org.jvnet.ws.wadl.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Controller
@RequestMapping("/v2/application.wadl")
public class WADLControllerV2 {
String xs_namespace="http://www.w3.org/2001/XMLSchema" ;
@Autowired
private RequestMappingHandlerMapping handlerMapping;
@Autowired
private WebApplicationContext webApplicationContext;

@RequestMapping(method=RequestMethod.GET, produces={"application/xml"} )
public @ResponseBody Application generateWadl(HttpServletRequest request) {
Application result = new Application();
Doc doc = new Doc();
doc.setTitle("Spring REST Service WADL");
result.getDoc().add(doc);
Resources wadResources = new Resources();
wadResources.setBase(getBaseUrl(request));

Map<RequestMappingInfo, HandlerMethod> handletMethods = handlerMapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handletMethods.entrySet()) {


HandlerMethod handlerMethod = entry.getValue();

Object object = handlerMethod.getBean();
Object bean = webApplicationContext.getBean(object.toString());

boolean isRestContoller = bean.getClass().isAnnotationPresent(RestController.class);
if(!isRestContoller) {
continue;
}
RequestMappingInfo mappingInfo = entry.getKey();

Set<String> pattern = mappingInfo.getPatternsCondition().getPatterns();
Set<RequestMethod> httpMethods = mappingInfo.getMethodsCondition().getMethods();
ProducesRequestCondition producesRequestCondition = mappingInfo.getProducesCondition();
Set<MediaType> mediaTypes = producesRequestCondition.getProducibleMediaTypes();
Resource wadlResource = null;
for (RequestMethod httpMethod : httpMethods) {
org.jvnet.ws.wadl.Method wadlMethod = new org.jvnet.ws.wadl.Method();

for (String uri : pattern) {
wadlResource = createOrFind( uri, wadResources);
wadlResource.setPath(uri);
}

wadlMethod.setName(httpMethod.name());
Method javaMethod = handlerMethod.getMethod();
wadlMethod.setId(javaMethod.getName());
Doc wadlDocMethod = new Doc();
wadlDocMethod.setTitle(javaMethod.getDeclaringClass().getSimpleName()+"."+javaMethod.getName());
wadlMethod.getDoc().add(wadlDocMethod);

// Request
Request wadlRequest = new Request();

Annotation[][] annotations = javaMethod.getParameterAnnotations();
Class<?>[] paramTypes = javaMethod.getParameterTypes();
int i = 0;
for (Annotation[] annotation : annotations) {
Class<?> paramType =paramTypes[i];
i++;
for (Annotation annotation2 : annotation) {

if (annotation2 instanceof RequestParam ) {
RequestParam param2 = (RequestParam)annotation2;
Param waldParam = new Param();
QName nm = convertJavaToXMLType(paramType);
waldParam.setName(param2.value());
waldParam.setStyle(ParamStyle.QUERY);
waldParam.setRequired(param2.required());
String defaultValue = cleanDefault(param2.defaultValue());
if ( !defaultValue.equals("") ) {
waldParam.setDefault(defaultValue);
}
waldParam.setType(nm);
wadlRequest.getParam().add(waldParam);
} else if ( annotation2 instanceof PathVariable ) {
PathVariable param2 = (PathVariable)annotation2;
QName nm = convertJavaToXMLType(paramType);
Param waldParam = new Param();
waldParam.setName(param2.value());
waldParam.setStyle(ParamStyle.TEMPLATE);
waldParam.setRequired(true);
wadlRequest.getParam().add(waldParam);
waldParam.setType(nm);
}
}
}
if ( ! wadlRequest.getParam().isEmpty() ) {
wadlMethod.setRequest(wadlRequest);
}

// Response
if ( !mediaTypes.isEmpty() ) {
Response wadlResponse = new Response();
Class methodReturn = handlerMethod.getReturnType().getClass();
ResponseStatus status = handlerMethod.getMethodAnnotation(ResponseStatus.class);
if(status==null) {
wadlResponse.getStatus().add((long)(HttpStatus.OK.value()));
}else {
HttpStatus httpcode = status.value();
wadlResponse.getStatus().add((long)httpcode.value());
}

for (MediaType mediaType : mediaTypes) {
Representation wadlRepresentation = new Representation();
wadlRepresentation.setMediaType(mediaType.toString());
wadlResponse.getRepresentation().add(wadlRepresentation);
}
wadlMethod.getResponse().add(wadlResponse);
}

wadlResource.getMethodOrResource().add(wadlMethod);

}



}
result.getResources().add(wadResources);

return result;
}
private QName convertJavaToXMLType(Class<?> type) {
QName nm = new QName("");
String classname=type.toString();
if (classname.indexOf("String")>=0) {
nm = new QName(xs_namespace,"string","xs");

}else if(classname.indexOf("Integer")>=0) {
nm = new QName(xs_namespace,"int","xs");
}
return nm;
}
private Resource createOrFind(String uri, Resources wadResources) {
List<Resource> current = wadResources.getResource();
for(Resource resource:current) {
if(resource.getPath().equalsIgnoreCase(uri)){
return resource;
}
}
Resource wadlResource = new Resource();
current.add(wadlResource);
return wadlResource;
}
private String getBaseUrl (HttpServletRequest request) {
String requestUri = request.getRequestURI();
return request.getScheme()+"://"+ request.getServerName()+":"+ request.getServerPort() + requestUri;
}

private String cleanDefault(String value) {
value = value.replaceAll("t", "");
value = value.replaceAll("n", "");
return value;
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.