Search in sources :

Example 1 with RequestBody

use of org.apache.tapestry5.annotations.RequestBody in project tapestry-5 by apache.

the class OnEventWorker method createRequestBodyProvider.

@SuppressWarnings({ "unchecked", "rawtypes" })
private EventHandlerMethodParameterProvider createRequestBodyProvider(PlasticMethod method, final int parameterIndex, final String parameterTypeName, final boolean allowEmpty) {
    final String methodIdentifier = method.getMethodIdentifier();
    return (event) -> {
        Invokable<Object> operation = () -> {
            Class parameterType = classCache.forName(parameterTypeName);
            Optional result = restSupport.getRequestBodyAs(parameterType);
            if (!allowEmpty && !result.isPresent()) {
                throw new RuntimeException(String.format("The request has an empty body and %s has one parameter with @RequestBody(allowEmpty=false)", methodIdentifier));
            }
            return result.orElse(null);
        };
        return operationTracker.invoke("Converting HTTP request body for @RequestBody parameter", operation);
    };
}
Also used : PlasticField(org.apache.tapestry5.plastic.PlasticField) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) Flow(org.apache.tapestry5.func.Flow) JSONObject(org.apache.tapestry5.json.JSONObject) MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) ComponentClassCache(org.apache.tapestry5.internal.services.ComponentClassCache) ExceptionUtils(org.apache.tapestry5.commons.util.ExceptionUtils) PublishServerSideEvents(org.apache.tapestry5.corelib.mixins.PublishServerSideEvents) CollectionFactory(org.apache.tapestry5.commons.util.CollectionFactory) Map(java.util.Map) TransformConstants(org.apache.tapestry5.services.TransformConstants) LocalVariable(org.apache.tapestry5.plastic.LocalVariable) JSONArray(org.apache.tapestry5.json.JSONArray) ValueEncoder(org.apache.tapestry5.ValueEncoder) Event(org.apache.tapestry5.runtime.Event) PageLifecycleListener(org.apache.tapestry5.runtime.PageLifecycleListener) Predicate(org.apache.tapestry5.func.Predicate) Set(java.util.Set) Invokable(org.apache.tapestry5.ioc.Invokable) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) MutableComponentModel(org.apache.tapestry5.model.MutableComponentModel) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) List(java.util.List) ComponentClassTransformWorker2(org.apache.tapestry5.services.transform.ComponentClassTransformWorker2) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) EventContext(org.apache.tapestry5.EventContext) InternalConstants(org.apache.tapestry5.internal.InternalConstants) RequestBody(org.apache.tapestry5.annotations.RequestBody) PublishEvent(org.apache.tapestry5.annotations.PublishEvent) MethodParameter(org.apache.tapestry5.plastic.MethodParameter) Request(org.apache.tapestry5.http.services.Request) OnEvent(org.apache.tapestry5.annotations.OnEvent) LocalVariableCallback(org.apache.tapestry5.plastic.LocalVariableCallback) ComponentResources(org.apache.tapestry5.ComponentResources) RequestParameter(org.apache.tapestry5.annotations.RequestParameter) ValueEncoderSource(org.apache.tapestry5.services.ValueEncoderSource) RestSupport(org.apache.tapestry5.http.services.RestSupport) StaticActivationContextValue(org.apache.tapestry5.annotations.StaticActivationContextValue) PlasticMethod(org.apache.tapestry5.plastic.PlasticMethod) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) Condition(org.apache.tapestry5.plastic.Condition) MethodDescription(org.apache.tapestry5.plastic.MethodDescription) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) TransformationSupport(org.apache.tapestry5.services.transform.TransformationSupport) OperationTracker(org.apache.tapestry5.ioc.OperationTracker) DisableStrictChecks(org.apache.tapestry5.annotations.DisableStrictChecks) F(org.apache.tapestry5.func.F) InternalUtils(org.apache.tapestry5.ioc.internal.util.InternalUtils) Mapper(org.apache.tapestry5.func.Mapper) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) Optional(java.util.Optional) Invokable(org.apache.tapestry5.ioc.Invokable) PlasticClass(org.apache.tapestry5.plastic.PlasticClass)

Example 2 with RequestBody

use of org.apache.tapestry5.annotations.RequestBody in project tapestry-5 by apache.

the class DefaultOpenApiDescriptionGenerator method processRequestBody.

private void processRequestBody(Method method, final String uri, final String httpMethod, final JSONObject methodDescription, JSONArray parametersAsJsonArray, Parameter parameter) {
    JSONObject requestBodyDescription = new JSONObject();
    requestBodyDescription.put("required", !(parameter.getAnnotation(RequestBody.class).allowEmpty()));
    getValue(method, uri, httpMethod, "requestbody.description").ifPresent((v) -> requestBodyDescription.put("description", v));
    RestInfo restInfo = method.getAnnotation(RestInfo.class);
    if (restInfo != null) {
        JSONObject contentDescription = new JSONObject();
        for (String contentType : restInfo.consumes()) {
            JSONObject schemaDescription = new JSONObject();
            typeDescriber.describe(schemaDescription, parameter);
            schemaDescription.remove("required");
            contentDescription.put(contentType, schemaDescription);
        }
        requestBodyDescription.put("content", contentDescription);
    }
    methodDescription.put("requestBody", requestBodyDescription);
}
Also used : JSONObject(org.apache.tapestry5.json.JSONObject) RestInfo(org.apache.tapestry5.annotations.RestInfo)

Example 3 with RequestBody

use of org.apache.tapestry5.annotations.RequestBody in project tapestry-5 by apache.

the class Index method onHttpPut.

@RestInfo(consumes = "application/json")
public Object onHttpPut(@RequestBody User user) throws UnsupportedEncodingException {
    HttpStatus status;
    if (!USERS.contains(user)) {
        USERS.add(user);
        status = HttpStatus.created();
    } else {
        status = HttpStatus.ok();
    }
    return status.withContentLocation(pageRenderLinkSource.createPageRenderLinkWithContext(Index.class, user.getEmail()));
}
Also used : HttpStatus(org.apache.tapestry5.services.HttpStatus) RestInfo(org.apache.tapestry5.annotations.RestInfo)

Aggregations

RestInfo (org.apache.tapestry5.annotations.RestInfo)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 Array (java.lang.reflect.Array)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 ComponentResources (org.apache.tapestry5.ComponentResources)1 EventContext (org.apache.tapestry5.EventContext)1 ValueEncoder (org.apache.tapestry5.ValueEncoder)1 DisableStrictChecks (org.apache.tapestry5.annotations.DisableStrictChecks)1 OnEvent (org.apache.tapestry5.annotations.OnEvent)1 PublishEvent (org.apache.tapestry5.annotations.PublishEvent)1 RequestBody (org.apache.tapestry5.annotations.RequestBody)1 RequestParameter (org.apache.tapestry5.annotations.RequestParameter)1 StaticActivationContextValue (org.apache.tapestry5.annotations.StaticActivationContextValue)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 CollectionFactory (org.apache.tapestry5.commons.util.CollectionFactory)1