Search in sources :

Example 1 with TypeHandler

use of org.raml.jaxrs.plugins.TypeHandler in project raml-for-jax-rs by mulesoft-labs.

the class ModelEmitter method writeMultiPartFormData.

private void writeMultiPartFormData(RamlResourceMethod method, BodyBuilder body) throws IOException {
    TypeBuilder typeBuilder = TypeBuilder.type("object");
    List<RamlMultiFormDataParameter> formData = method.getMultiFormDataParameter();
    for (RamlMultiFormDataParameter formDatum : formData) {
        Type type = formDatum.getPartEntity().getType();
        TypeHandler typeHandler = pickTypeHandler(type);
        typeBuilder.withProperty(TypePropertyBuilder.property(formDatum.getName(), typeHandler.writeType(typeRegistry, formDatum.getPartEntity())));
    }
    body.ofType(typeBuilder);
}
Also used : Type(java.lang.reflect.Type) RamlToPojoTypeHandler(org.raml.emitter.plugins.RamlToPojoTypeHandler) TypeHandler(org.raml.jaxrs.plugins.TypeHandler)

Example 2 with TypeHandler

use of org.raml.jaxrs.plugins.TypeHandler in project raml-for-jax-rs by mulesoft-labs.

the class ModelEmitter method writeQueryParameters.

private void writeQueryParameters(Iterable<RamlQueryParameter> queryParameters, MethodBuilder builder) throws IOException {
    for (RamlQueryParameter parameter : queryParameters) {
        TypeHandler typeHandler = pickTypeHandler(parameter.getEntity().getType());
        ParameterEmitter parameterEmitter = new ParameterEmitter(typeRegistry, typeHandler);
        ParameterBuilder parameterBuilder = parameterEmitter.emit(parameter);
        builder.withQueryParameter(parameterBuilder);
    }
}
Also used : RamlToPojoTypeHandler(org.raml.emitter.plugins.RamlToPojoTypeHandler) TypeHandler(org.raml.jaxrs.plugins.TypeHandler) ParameterEmitter(org.raml.jaxrs.emitters.ParameterEmitter)

Example 3 with TypeHandler

use of org.raml.jaxrs.plugins.TypeHandler in project raml-for-jax-rs by mulesoft-labs.

the class ModelEmitter method writeHeaderParameters.

private void writeHeaderParameters(Iterable<RamlHeaderParameter> headerParameters, MethodBuilder builder) throws IOException {
    for (RamlHeaderParameter parameter : headerParameters) {
        TypeHandler typeHandler = pickTypeHandler(parameter.getEntity().getType());
        ParameterEmitter parameterEmitter = new ParameterEmitter(typeRegistry, typeHandler);
        ParameterBuilder parameterBuilder = parameterEmitter.emit(parameter);
        builder.withHeaderParameters(parameterBuilder);
    }
}
Also used : RamlToPojoTypeHandler(org.raml.emitter.plugins.RamlToPojoTypeHandler) TypeHandler(org.raml.jaxrs.plugins.TypeHandler) ParameterEmitter(org.raml.jaxrs.emitters.ParameterEmitter)

Example 4 with TypeHandler

use of org.raml.jaxrs.plugins.TypeHandler in project raml-for-jax-rs by mulesoft-labs.

the class DefaultResponseHandler method writeResponses.

@Override
public void writeResponses(TypeRegistry typeRegistry, RamlResourceMethod method, TypeSelector selector, MethodBuilder methodBuilder) throws IOException {
    if (!method.getProducedType().isPresent()) {
        return;
    }
    // We have no clue what the error responses are, however, we want to generate
    // well formed raml, so we pick one.
    ResponseBuilder responseBuilder = ResponseBuilder.response(200);
    for (RamlMediaType producedMediaType : method.getProducedMediaTypes()) {
        BodyBuilder body = BodyBuilder.body(producedMediaType.toStringRepresentation());
        responseBuilder.withBodies(body);
        TypeHandler typeHandler = selector.pickTypeWriter(method, producedMediaType);
        TypeBuilder type = typeHandler.writeType(typeRegistry, method.getProducedType().get());
        body.ofType(type);
    }
    methodBuilder.withResponses(responseBuilder);
}
Also used : TypeBuilder(org.raml.builder.TypeBuilder) RamlMediaType(org.raml.api.RamlMediaType) ResponseBuilder(org.raml.builder.ResponseBuilder) TypeHandler(org.raml.jaxrs.plugins.TypeHandler) BodyBuilder(org.raml.builder.BodyBuilder)

Example 5 with TypeHandler

use of org.raml.jaxrs.plugins.TypeHandler in project raml-for-jax-rs by mulesoft-labs.

the class ModelEmitter method writeMethod.

private void writeMethod(ResourceBuilder resourceBuilder, RamlResourceMethod method) throws IOException {
    MethodBuilder methodBuilder = MethodBuilder.method(method.getHttpMethod());
    ModelEmitterAnnotations.annotate(supportedAnnotations, method, methodBuilder);
    Optional<String> description = method.getDescription();
    if (description.isPresent() && !description.get().isEmpty()) {
        methodBuilder.with(property("description", description.get()));
    }
    if (!method.getConsumedMediaTypes().isEmpty() && (method.getConsumedType().isPresent() || !method.getMultiFormDataParameter().isEmpty() || !method.getFormParameters().isEmpty())) {
        for (RamlMediaType ramlMediaType : method.getConsumedMediaTypes()) {
            BodyBuilder body = body(ramlMediaType.toStringRepresentation());
            methodBuilder.withBodies(body);
            if (ramlMediaType.toStringRepresentation().equals("multipart/form-data")) {
                writeMultiPartFormData(method, body);
            } else {
                if (ramlMediaType.toStringRepresentation().equals("application/x-www-form-urlencoded")) {
                    writeFormParam(method, body);
                } else {
                    Type type = method.getConsumedType().get().getType();
                    TypeHandler typeHandler = pickTypeHandler(type);
                    body.ofType(typeHandler.writeType(typeRegistry, method.getConsumedType().get()));
                }
            }
        }
    }
    ResponseHandler handler = pickResponseHandler(method);
    TypeSelector selector = new TypeSelector() {

        @Override
        public TypeHandler pickTypeWriter(RamlResourceMethod method, RamlMediaType producedMediaType) throws IOException {
            return pickTypeHandler(method.getProducedType().get().getType());
        }
    };
    if (!method.getProducedMediaTypes().isEmpty()) {
        handler.writeResponses(typeRegistry, method, selector, methodBuilder);
    }
    if (!method.getHeaderParameters().isEmpty()) {
        writeHeaderParameters(method.getHeaderParameters(), methodBuilder);
    }
    if (!method.getQueryParameters().isEmpty()) {
        writeQueryParameters(method.getQueryParameters(), methodBuilder);
    }
    resourceBuilder.with(methodBuilder);
}
Also used : Type(java.lang.reflect.Type) DefaultResponseHandler(org.raml.emitter.plugins.DefaultResponseHandler) ResponseHandler(org.raml.emitter.plugins.ResponseHandler) RamlToPojoTypeHandler(org.raml.emitter.plugins.RamlToPojoTypeHandler) TypeHandler(org.raml.jaxrs.plugins.TypeHandler) TypeSelector(org.raml.jaxrs.plugins.TypeSelector)

Aggregations

TypeHandler (org.raml.jaxrs.plugins.TypeHandler)5 RamlToPojoTypeHandler (org.raml.emitter.plugins.RamlToPojoTypeHandler)4 Type (java.lang.reflect.Type)2 ParameterEmitter (org.raml.jaxrs.emitters.ParameterEmitter)2 RamlMediaType (org.raml.api.RamlMediaType)1 BodyBuilder (org.raml.builder.BodyBuilder)1 ResponseBuilder (org.raml.builder.ResponseBuilder)1 TypeBuilder (org.raml.builder.TypeBuilder)1 DefaultResponseHandler (org.raml.emitter.plugins.DefaultResponseHandler)1 ResponseHandler (org.raml.emitter.plugins.ResponseHandler)1 TypeSelector (org.raml.jaxrs.plugins.TypeSelector)1