act.controller.Controller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of act Show documentation
Show all versions of act Show documentation
The ACT full stack MVC framework
package act.controller;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import act.app.ActionContext;
import act.conf.AppConfigKey;
import act.controller.meta.HandlerMethodMetaInfo;
import act.data.Versioned;
import act.route.Router;
import act.util.*;
import act.util.JsonUtilConfig.JsonWriter;
import act.view.*;
import com.alibaba.fastjson.JSON;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.mvc.result.*;
import org.osgl.storage.ISObject;
import org.osgl.util.*;
import org.osgl.util.Output;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.lang.annotation.*;
import java.net.URL;
import java.util.Map;
import javax.inject.Inject;
/**
* Mark a class as Controller, which contains at least one of the following:
*
* - Action handler method
* - Any one of Before/After/Exception/Finally interceptor
*
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface Controller {
/**
* Indicate the context path for all action methods declared
* in this controller.
*
* Default value: "{@code /}"
*
* @return the controller context path
*/
String value() default "/";
/**
* Specify the port(s) this controller's action method shall be
* routed from.
*
* @return the port name
* @see AppConfigKey#NAMED_PORTS
*/
String[] port() default {};
/**
* Provides utilities for controller action methods to emit rendering results
*/
class Util {
public static final Ok OK = Ok.get();
public static final Created CREATED = Created.INSTANCE;
public static final Result CREATED_JSON = new Result(H.Status.CREATED, "{\"message\": \"Created\"}") {
};
public static final Result CREATED_XML = new Result(H.Status.CREATED, "Created ") {
};
public static final Result OK_JSON = new Result(H.Status.OK, "{\"message\": \"Okay\"}") {
};
public static final Result OK_XML = new Result(H.Status.OK, "Okay ") {
};
public static final NoContent NO_CONTENT = NoContent.get();
/**
* Returns an {@link Ok} result
*/
public static Result ok() {
H.Format accept = ActionContext.current().accept();
if (H.Format.JSON == accept) {
return OK_JSON;
} else if (H.Format.XML == accept) {
return OK_XML;
}
return OK;
}
/**
* Returns a {@link Created} result
*
* @param resourceGetUrl
* the URL to access the new resource been created
* @return the result as described
*/
public static Created created(String resourceGetUrl) {
return Created.withLocation(resourceGetUrl);
}
/**
* Return a {@link Created} result
*
* @return the result as described
*/
public static Created created() {
return Created.INSTANCE;
}
public static NotModified notModified() {
return NotModified.get();
}
public static NotModified notModified(String etag, Object... args) {
return NotModified.of(etag, args);
}
/**
* Returns a {@link Accepted} result
*
* @param statusMonitorUrl
* the URL to check the request process status
* @return the result as described
*/
public static Result accepted(String statusMonitorUrl) {
return new Accepted(statusMonitorUrl);
}
public static Result notAcceptable() {
return NotAcceptable.get();
}
public static Result notAcceptable(String msg, Object... args) {
return NotAcceptable.of(msg, args);
}
/**
* Returns an {@link NotFound} result
*/
public static Result notFound() {
return ActNotFound.create();
}
/**
* Returns an {@link NotFound} result with custom message
* template and arguments. The final message is rendered with
* the template and arguments using {@link String#format(String, Object...)}
*
* @param msg
* the message template
* @param args
* the message argument
*/
public static Result notFound(String msg, Object... args) {
return ActNotFound.create(msg, args);
}
/**
* Check if the input is `null` then throw out `NotFound` result, otherwise return
* the input back.
*
* @param o
* the input object to be evaluated.
* @param
* the type parameter.
* @return
* the input `o` if it is not `null`.
*/
public static T requireNotNull(T o) {
notFoundIfNull(o);
return o;
}
/**
* Throws out an {@link NotFound} result if the object specified is
* {@code null}
*
* @param o
* the object to be evaluated
*/
public static T notFoundIfNull(T o) {
if (null == o) {
throw ActNotFound.create();
}
return o;
}
/**
* Throws out an {@link NotFound} result with custom message template and
* arguments if the object specified is {@code null}. The final message is
* rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param o
* the object to be evaluated
* @param msg
* the message template
* @param args
* the message argument
*/
public static T notFoundIfNull(T o, String msg, Object... args) {
if (null == o) {
throw ActNotFound.create(msg, args);
}
return o;
}
/**
* Throws out an {@link NotFound} result if the boolean expression specified
* is {@code true}
* {@code null}
*
* @param test
* the boolean expression to be evaluated
*/
public static void notFoundIf(boolean test) {
if (test) {
throw ActNotFound.create();
}
}
/**
* Throws out an {@link NotFound} result with custom message template and
* arguments if the expression specified is {@code true}. The final message is
* rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param test
* the boolean expression
* @param msg
* the message template
* @param args
* the message argument
*/
public static void notFoundIf(boolean test, String msg, Object... args) {
if (test) {
throw ActNotFound.create(msg, args);
}
}
/**
* Throws out an {@link NotFound} result if the boolean expression specified
* is {@code false}
* {@code null}
*
* @param test
* the boolean expression to be evaluated
*/
public static void notFoundIfNot(boolean test) {
notFoundIf(!test);
}
/**
* Throws out an {@link NotFound} result with custom message template and
* arguments if the expression specified is {@code false}. The final message is
* rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param test
* the boolean expression
* @param msg
* the message template
* @param args
* the message argument
*/
public static void notFoundIfNot(boolean test, String msg, Object... args) {
notFoundIf(!test, msg, args);
}
/**
* Returns a {@link BadRequest} instance.
*
* @return a bad request response
*/
public static BadRequest badRequest() {
return ActBadRequest.create();
}
/**
* Returns a {@link BadRequest} instance along with error message.
*
* @param msg
* the message pattern
* @param args
* the message argument
* @return a bad request with error message
*/
public static BadRequest badRequest(String msg, Object... args) {
return ActBadRequest.create(msg, args);
}
/**
* Throws out a {@link BadRequest} if test evaluated to `true`.
*
* @param test
* the test
*/
public static void badRequestIf(boolean test) {
if (test) {
throw ActBadRequest.create();
}
}
/**
* Throws out a {@link BadRequest} with error message if test evaluated to `true`.
*
* @param test
* the test
* @param msg
* the error message pattern
* @param args
* the error message arguments
*/
public static void badRequestIf(boolean test, String msg, Object... args) {
if (test) {
throw ActBadRequest.create(msg, args);
}
}
/**
* Throws out a {@link BadRequest} if test string {@link S#blank(String)}.
*
* @param test
* the string to be checked
*/
public static void badRequestIfBlank(String test) {
if (S.blank(test)) {
throw ActBadRequest.create();
}
}
/**
* Throws out a {@link BadRequest} with error message if test string is blank
*
* @param test
* the string to be checked
* @param msg
* the error message pattern
* @param args
* the error message argument
*/
public static void badRequestIfBlank(String test, String msg, Object... args) {
if (S.blank(test)) {
throw ActBadRequest.create(msg, args);
}
}
/**
* Throws out a {@link BadRequest} if test object is `null`
*
* @param test
* the object to be checked
*/
public static void badRequestIfNull(Object test) {
if (null == test) {
throw ActBadRequest.create();
}
}
/**
* Throws out a {@link BadRequest} with error message if test object is null
*
* @param test
* the object to be checked
* @param msg
* the error message pattern
* @param args
* the error message argument
*/
public static void badRequestIfNull(Object test, String msg, Object... args) {
if (null == test) {
throw ActBadRequest.create(msg, args);
}
}
/**
* Throws out a {@link BadRequest} if test evaluated to `false`.
*
* @param test
* the test
*/
public static void badRequestIfNot(boolean test) {
if (!test) {
throw ActBadRequest.create();
}
}
/**
* Throws out a {@link BadRequest} with error message if test evaluated to `false`.
*
* @param test
* the test
* @param msg
* the error message pattern
* @param args
* the error message arguments
*/
public static void badRequestIfNot(boolean test, String msg, Object... args) {
badRequestIf(!test, msg, args);
}
/**
* Returns a {@link BadRequest} instance with error code specified.
*
* @param code
* the error code
* @return the bad request instance
*/
public static BadRequest badRequest(int code) {
return ActBadRequest.create(code);
}
/**
* Returns a {@link BadRequest} instance with error code and message specified.
*
* @param code
* the error code
* @param msg
* the error message pattern
* @param args
* the error message arguments
* @return the bad request
*/
public static BadRequest badRequest(int code, String msg, Object... args) {
return ActBadRequest.create(code, msg, args);
}
/**
* Throws out a {@link BadRequest} with error code if `test` evaluated to `true`.
*
* @param test
* the test
* @param code
* the error code
*/
public static void badRequestIf(boolean test, int code) {
if (test) {
throw ActBadRequest.create(code);
}
}
/**
* Throws out a {@link BadRequest} with error code and message if `test` evaluated to `true`.
*
* @param test
* the test
* @param msg
* the error message pattern
* @param args
* the error message arguments
* @param code
* the error code
*/
public static void badRequestIf(boolean test, int code, String msg, Object... args) {
if (test) {
throw ActBadRequest.create(code, msg, args);
}
}
/**
* Throws out a {@link BadRequest} with error code if `test` string {@link S#blank(String) is blank}.
*
* @param test
* the test string
* @param code
* the error code
*/
public static void badRequestIfBlank(String test, int code) {
if (S.blank(test)) {
throw ActBadRequest.create(code);
}
}
/**
* Throws out a {@link BadRequest} with error code and message if `test` string {@link S#blank(String) is blank}.
*
* @param test
* the test string
* @param msg
* the error message pattern
* @param args
* the error message arguments
* @param code
* the error code
*/
public static void badRequestIfBlank(String test, int code, String msg, Object... args) {
if (S.blank(test)) {
throw ActBadRequest.create(code, msg, args);
}
}
/**
* Throws out a {@link BadRequest} with error code if `test` is `null`.
*
* @param test
* the test object
* @param code
* the error code
*/
public static void badRequestIfNull(Object test, int code) {
if (null == test) {
throw ActBadRequest.create(code);
}
}
/**
* Throws out a {@link BadRequest} with error code and message if `test` is `null`.
*
* @param test
* the test object
* @param msg
* the error message pattern
* @param args
* the error message arguments
* @param code
* the error code
*/
public static void badRequestIfNull(Object test, int code, String msg, Object... args) {
if (null == test) {
throw ActBadRequest.create(code, msg, args);
}
}
/**
* Throws out a {@link BadRequest} with error code if `test` is `false`.
*
* @param test
* the test object
* @param code
* the error code
*/
public static void badRequestIfNot(boolean test, int code) {
if (!test) {
throw ActBadRequest.create(code);
}
}
/**
* Throws out a {@link BadRequest} with error code and message if `test` is `false`.
*
* @param test
* the test object
* @param msg
* the error message pattern
* @param args
* the error message arguments
* @param code
* the error code
*/
public static void badRequestIfNot(boolean test, int code, String msg, Object... args) {
badRequestIf(!test, code, msg, args);
}
/**
* Returns a {@link Conflict} instance.
*
* @return a 409 Conflict result
*/
public static Conflict conflict() {
return ActConflict.create();
}
/**
* Returns a {@link Conflict} instance with error message.
*
* @param message
* the error message pattern
* @param args
* the error message arguments
* @return a 409 Conflict result
*/
public static Conflict conflict(String message, Object... args) {
return ActConflict.create(message, args);
}
/**
* Throws out a {@link Conflict} if `test` is `true`.
*
* @param test
* the test
*/
public static void conflictIf(boolean test) {
if (test) {
throw ActConflict.create();
}
}
/**
* Throws out a {@link Conflict} with error message specified if `test` is `true`.
*
* @param test
* the test
* @param message
* the error message pattern
* @param args
* the error message arguments
*/
public static void conflictIf(boolean test, String message, Object... args) {
if (test) {
throw ActConflict.create(message, args);
}
}
/**
* Throws out a {@link Conflict} if `test` is `false`.
*
* @param test
* the test
*/
public static void conflictIfNot(boolean test) {
conflictIf(!test);
}
/**
* Throws out a {@link Conflict} with error message specified if `test` is `false`.
*
* @param test
* the test
* @param message
* the error message pattern
* @param args
* the error message arguments
*/
public static void conflictIfNot(boolean test, String message, Object... args) {
conflictIf(!test, message, args);
}
/**
* Returns a {@link Conflict} instance with error code.
*
* @param code
* the error message code
* @return a 409 Conflict result
*/
public static Conflict conflict(int code) {
return ActConflict.create(code);
}
/**
* Returns a {@link Conflict} instance with error code and message specified.
*
* @param code
* the error message code
* @param message
* the error message pattern
* @param args
* the error message arguments
* @return a 409 Conflict result
*/
public static Conflict conflict(int code, String message, Object... args) {
return ActConflict.create(code, message, args);
}
/**
* Throws out a {@link Conflict} with error code if `test` is `true`.
*
* @param test
* the test
* @param code
* the error code
*/
public static void conflictIf(boolean test, int code) {
if (test) {
throw ActConflict.create(code);
}
}
/**
* Throws out a {@link Conflict} with error code and message if `test` is `true`.
*
* @param test
* the test
* @param message
* the error message pattern
* @param args
* the error message arguments
* @param code
* the error code
*/
public static void conflictIf(boolean test, int code, String message, Object... args) {
if (test) {
throw ActConflict.create(code, message, args);
}
}
/**
* Throws out a {@link Conflict} with error code if `test` is `false`.
*
* @param test
* the test
* @param code
* the error code
*/
public static void conflictIfNot(boolean test, int code) {
conflictIf(!test, code);
}
/**
* Throws out a {@link Conflict} with error code and message if `test` is `false`.
*
* @param test
* the test
* @param message
* the error message pattern
* @param args
* the error message arguments
* @param code
* the error code
*/
public static void conflictIfNot(boolean test, int code, String message, Object... args) {
conflictIf(!test, code, message, args);
}
/**
* Returns a {@link Unauthorized} instance.
*
* @return a 401 Unauthorized result
*/
public static Unauthorized unauthorized() {
return ActUnauthorized.create();
}
/**
* Returns a {@link Unauthorized} instance with error code specified.
*
* @param errorCode
* the application error code.
* @return a 401 Unauthorized result
*/
public static Unauthorized unauthorized(int errorCode) {
return ActUnauthorized.create(errorCode);
}
/**
* Returns a {@link Unauthorized} instance with error code specified.
*
* @param errorCode
* the application error code.
* @param message
* the application error message.
* @param args
* the message template arguments
* @return a 401 Unauthorized result
*/
public static Unauthorized unauthorized(int errorCode, String message, Object ... args) {
return ActUnauthorized.create(errorCode, S.fmt(message, args));
}
/**
* Returns a {@link Unauthorized} instance with `realm` specified.
*
* @param realm
* the realm of the `Unauthorized` response
* @return a 401 Unauthorized result
*/
public static Unauthorized unauthorized(String realm) {
return ActUnauthorized.create(realm);
}
/**
* Returns a {@link Unauthorized} instance with `realm` and `digest` specified.
*
* @param realm
* the realm of the `Unauthorized` response
* @param digest
* whether apply digest
* @return a 401 Unauthorized result
*/
public static Unauthorized unauthorized(String realm, boolean digest) {
return ActUnauthorized.create(realm, digest);
}
/**
* Throws out an {@link Unauthorized} instance if `test ` is `true`.
*
* @param test
* the test
*/
public static void unauthorizedIf(boolean test) {
if (test) {
throw ActUnauthorized.create();
}
}
/**
* Throws out an {@link Unauthorized} instance if `test ` is `true`.
*
* @param test
* the test
* @param code
* the app specified error code
*/
public static void unauthorizedIf(boolean test, int code) {
if (test) {
throw ActUnauthorized.create(code);
}
}
/**
* Throws out an {@link Unauthorized} instance if `test ` is `true`.
*
* @param test
* the test
* @param code
* the app specified error code
* @param message
* the app specified error message (template)
* @param args
* the error message template arguments
*/
public static void unauthorizedIf(boolean test, int code, String message, Object ... args) {
if (test) {
throw ActUnauthorized.create(code, S.fmt(message, args));
}
}
/**
* Throws out an {@link Unauthorized} instance with `realm` specified if `test ` is `true`.
*
* @param test
* the test
* @param realm
* the realm
*/
public static void unauthorizedIf(boolean test, String realm) {
if (test) {
throw ActUnauthorized.create(realm);
}
}
/**
* Throws out an {@link Unauthorized} instance with `realm` and `digest` specified if `test ` is `true`.
*
* @param test
* the test
* @param realm
* the realm
* @param digest
* whether apply digest
*/
public static void unauthorizedIf(boolean test, String realm, boolean digest) {
if (test) {
throw ActUnauthorized.create(realm, digest);
}
}
/**
* Throws out an {@link Unauthorized} instance if `test ` is `false`.
*
* @param test
* the test
*/
public static void unauthorizedIfNot(boolean test) {
unauthorizedIf(!test);
}
/**
* Throws out an {@link Unauthorized} instance with `realm` specified if `test ` is `false`.
*
* @param test
* the test
* @param realm
* the realm
*/
public static void unauthorizedIfNot(boolean test, String realm) {
unauthorizedIf(!test, realm);
}
/**
* Throws out an {@link Unauthorized} instance with `realm` and `digest` specified if `test ` is `false`.
*
* @param test
* the test
* @param realm
* the realm
* @param digest
* whether apply digest
*/
public static void unauthorizedIfNot(boolean test, String realm, boolean digest) {
unauthorizedIf(!test, realm, digest);
}
/**
* Returns a {@link Forbidden} result
*/
public static Forbidden forbidden() {
return ActForbidden.create();
}
/**
* Returns a {@link Forbidden} result with custom message
* template and arguments. The final message is rendered with
* the template and arguments using {@link String#format(String, Object...)}
*
* @param msg
* the message template
* @param args
* the message argument
*/
public static Forbidden forbidden(String msg, Object... args) {
return ActForbidden.create(msg, args);
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code true}
*
* @param test
* the test condition
*/
public static void forbiddenIf(boolean test) {
if (test) {
throw ActForbidden.create();
}
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code false}
*
* @param test
* the test condition
*/
public static void forbiddenIfNot(boolean test) {
forbiddenIf(!test);
}
/**
* Throws a {@link Forbidden} result if test condition is {@code true}
*
* @param test
* the test condition
* @param msg
* the message format template
* @param args
* the message format arguments
*/
public static void forbiddenIf(boolean test, String msg, Object... args) {
if (test) {
throw ActForbidden.create(msg, args);
}
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code false}
*
* @param test
* the test condition
* @param msg
* the message format template
* @param args
* the message format arguments
*/
public static void forbiddenIfNot(boolean test, String msg, Object... args) {
forbiddenIf(!test, msg, args);
}
/**
* Returns a {@link Forbidden} result
*/
public static Forbidden forbidden(int code) {
return ActForbidden.create(code);
}
/**
* Returns a {@link Forbidden} result with custom message
* template and arguments. The final message is rendered with
* the template and arguments using {@link String#format(String, Object...)}
*
* @param msg
* the message template
* @param args
* the message argument
*/
public static Forbidden forbidden(String msg, int code, Object... args) {
return ActForbidden.create(msg, args);
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code true}
*
* @param test
* the test condition
*/
public static void forbiddenIf(boolean test, int code) {
if (test) {
throw ActForbidden.create(code);
}
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code false}
*
* @param test
* the test condition
*/
public static void forbiddenIfNot(boolean test, int code) {
forbiddenIf(!test, code);
}
/**
* Throws a {@link Forbidden} result if test condition is {@code true}
*
* @param test
* the test condition
* @param msg
* the message format template
* @param args
* the message format arguments
*/
public static void forbiddenIf(boolean test, int code, String msg, Object... args) {
if (test) {
throw ActForbidden.create(code, msg, args);
}
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code false}
*
* @param test
* the test condition
* @param msg
* the message format template
* @param args
* the message format arguments
*/
public static void forbiddenIfNot(boolean test, int code, String msg, Object... args) {
forbiddenIf(!test, code, msg, args);
}
/**
* Implement `RequestDispatcher.forward(String)` semantic. Calling this API is equivalent to
*
* ```java
* ActionContext ctx = ActionContext.current();
* ctx.forward(url, args);
* ```
*
* @param url
* the url template
* @param args
* the url argument
* @see ActionContext#forward(String, Object...)
*/
public static void forward(String url, Object... args) {
ActionContext ctx = ActionContext.current();
ctx.forward(url, args);
}
public static Redirect redirect(String url, Object... args) {
return Redirect.of(redirectUrl(url, args));
}
public static Redirect redirect(String url, Map reverseRoutingArguments) {
return Redirect.of(redirectUrl(url, reverseRoutingArguments));
}
public static void redirectIf(boolean test, String url, Object... args) {
if (test) {
throw redirect(url, args);
}
}
public static void redirectIfNot(boolean test, String url, Object... args) {
redirectIf(!test, url, args);
}
public static void redirectIf(boolean test, String url, Map reverseRoutingArguments) {
if (test) {
throw redirect(url, reverseRoutingArguments);
}
}
public static void redirectIfNot(boolean test, String url, Map reverseRoutingArguments) {
redirectIf(!test, url, reverseRoutingArguments);
}
public static MovedPermanently moved(String url, Object... args) {
return MovedPermanently.of(redirectUrl(url, args));
}
public static MovedPermanently moved(String url, Map reverseRoutingArguments) {
return MovedPermanently.of(redirectUrl(url, reverseRoutingArguments));
}
public static Found found(String url, Object... args) {
return Found.of(redirectUrl(url, args));
}
public static Found found(String url, Map reverseRoutingArguments) {
return Found.of(redirectUrl(url, reverseRoutingArguments));
}
public static void foundIf(boolean test, String url, Object... args) {
if (test) {
throw found(url, args);
}
}
public static void foundIfNot(boolean test, String url, Object... args) {
foundIf(!test, url, args);
}
public static SeeOther seeOther(String url, Object... args) {
return SeeOther.of(redirectUrl(url, args));
}
public static SeeOther seeOther(String url, Map reverseRoutingArguments) {
return SeeOther.of(redirectUrl(url, reverseRoutingArguments));
}
public static void seeOtherIf(boolean test, String url, Object... args) {
if (test) {
throw seeOther(url, args);
}
}
public static void seeOtherIfNot(boolean test, String url, Object... args) {
seeOtherIf(!test, url, args);
}
public static TemporaryRedirect temporaryRedirect(String url, Object... args) {
return TemporaryRedirect.of(redirectUrl(url, args));
}
public static TemporaryRedirect temporaryRedirect(String url, Map reverseRoutingArguments) {
return TemporaryRedirect.of(redirectUrl(url, reverseRoutingArguments));
}
public static PermanentRedirect permanentRedirect(String url, Object... args) {
return PermanentRedirect.of(redirectUrl(url, args));
}
public static PermanentRedirect permanentRedirect(String url, Map reverseRoutingArguments) {
return PermanentRedirect.of(redirectUrl(url, reverseRoutingArguments));
}
private static String redirectUrl(String url, Object... args) {
url = S.fmt(url, args);
if (url.contains(".") || url.contains("(")) {
String inferFullActionPath = Router.inferFullActionPath(url);
if (inferFullActionPath != url) {
url = ActionContext.current().router().reverseRoute(url);
}
} else {
if (!url.startsWith("/")) {
ActionContext context = ActionContext.current();
String urlContext = context.urlContext();
if (S.notBlank(urlContext)) {
url = S.pathConcat(urlContext, '/', url);
}
}
}
return url;
}
private static String redirectUrl(String url, Map reverseRoutingArguments) {
url = Router.inferFullActionPath(url);
url = ActionContext.current().router().reverseRoute(url, reverseRoutingArguments);
return url;
}
/**
* Returns a {@link RenderText} result with specified message template
* and args. The final message is rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param msg
* the message format template
* @param args
* the message format arguments
*/
public static RenderText text(String msg, Object... args) {
return RenderText.of(successStatus(), msg, args);
}
/**
* Alias of {@link #text(String, Object...)}
*
* @param msg
* the message format template
* @param args
* the message format arguments
* @return the result
*/
public static RenderText renderText(String msg, Object... args) {
return text(msg, args);
}
/**
* Returns a {@link RenderText} result with specified message template
* and args. The final message is rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param msg
* the message format template
* @param args
* the message format arguments
* @return the result
*/
public static RenderHtml html(String msg, Object... args) {
return RenderHtml.of(successStatus(), msg, args);
}
/**
* Alias of {@link #html(String, Object...)}
*
* @param msg
* the message format template
* @param args
* the message format arguments
* @return the result
*/
public static RenderHtml renderHtml(String msg, Object ... args) {
return html(msg, args);
}
/**
* Returns a {@link RenderJSON} result with specified message template
* and args. The final message is rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param msg
* the message format template
* @param args
* the message format arguments
* @return the result
*/
public static RenderJSON json(String msg, Object... args) {
return RenderJSON.of(successStatus(), msg, args);
}
/**
* Alias of {@link #json(String, Object...)}
*
* @param msg
* the message format template
* @param args
* the message format arguments
* @return the result
*/
public static RenderJSON renderJson(String msg, Object... args) {
return json(msg, args);
}
/**
* Returns a {@link RenderJSON} result with any object. This method will
* call underline JSON serializer to transform the object into a JSON string
*
* @param data
* the data to be rendered as JSON string
* @return the result
*/
public static RenderJSON json(Object data) {
return RenderJSON.of(successStatus(), data);
}
/**
* Alias of {@link #json(Object)}
*
* @param data
* the data to be rendered as JSON string
* @return the result
*/
public static RenderJSON renderJson(Object data) {
return json(data);
}
/**
* Returns a {@link RenderJsonMap} result with any object. This method will
* generate a JSON object out from the {@link ActionContext#renderArgs}.
* The response is always in JSON format and ignores the HTTP `Accept`
* header setting
*
* @param data
* the varargs of Object to be put into the JSON map
* @return the result
*/
public static RenderJsonMap jsonMap(Object... data) {
return RenderJsonMap.get();
}
/**
* Alias of {@link #jsonMap(Object...)}
*
* @param data
* the data to be put into the JSON map
* @return the result
*/
public static RenderJsonMap renderJsonMap(Object... data) {
return jsonMap(data);
}
public static RenderJwt renderJwt() {
return RenderJwt.get();
}
public static RenderJwt jwt() {
return RenderJwt.get();
}
/**
* Returns a {@link RenderXML} result with specified message template
* and args. The final message is rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param msg
* the message format template
* @param args
* the message format arguments
* @return the result
*/
public static RenderXML xml(String msg, Object... args) {
return RenderXML.of(successStatus(), msg, args);
}
/**
* Alias of {@link #xml(String, Object...)}
*
* @param msg
* the message format template
* @param args
* the message format arguments
* @return the result
*/
public static RenderXML renderXml(String msg, Object... args) {
return xml(msg, args);
}
/**
* Returns a {@link RenderBinary} result with an {@link ISObject} instance. The result will render
* the binary using "inline" content disposition
*
* @param sobj
* the {@link ISObject} instance
* @return the result
*/
public static RenderBinary binary(ISObject sobj) {
return new RenderBinary(sobj.asInputStream(), sobj.getAttribute(ISObject.ATTR_FILE_NAME), sobj.getAttribute(ISObject.ATTR_CONTENT_TYPE), true);
}
/**
* Alias of {@link #binary(ISObject)}
*
* @param sobj
* the {@link ISObject} instance
* @return the result
*/
public static RenderBinary renderBinary(ISObject sobj) {
return binary(sobj);
}
/**
* Returns a {@link RenderBinary} result with an {@link ISObject} instance. The result will render
* the binary using "attachment" content disposition
*
* @param sobj
* the {@link ISObject} instance
*/
public static RenderBinary download(ISObject sobj) {
return new RenderBinary(sobj.asInputStream(), sobj.getAttribute(ISObject.ATTR_FILE_NAME), sobj.getAttribute(ISObject.ATTR_CONTENT_TYPE), false);
}
/**
* Returns a {@link RenderBinary} result with a file. The result will render
* the binary using "inline" content disposition.
*
* @param file
* the file to be rendered
* @return a result
*/
public static RenderBinary binary(File file) {
return new RenderBinary(file);
}
/**
* Alias of {@link #binary(File)}
*
* @param file
* the file to be rendered
* @return a result
*/
public static RenderBinary renderBinary(File file) {
return binary(file);
}
/**
* Returns a {@link RenderBinary} result with a delayed output stream writer.
* The result will render the binary using "inline" content disposition.
*
* @param outputStreamWriter
* the delayed writer
* @return the result
*/
public static RenderBinary binary($.Visitor