Search in sources :

Example 6 with JSONObjectBuilder

use of com.liferay.apio.architect.message.json.JSONObjectBuilder in project com-liferay-apio-architect by liferay.

the class ErrorWriter method writeError.

/**
 * Writes an {@link APIError} to a JSON object.
 *
 * @param  errorMessageMapper the {@link ErrorMessageMapper} that matches
 *         the {@code apiError} and {@code httpHeaders} parameters
 * @param  apiError the API error
 * @param  httpHeaders the current request's HTTP headers
 * @return the API error, as a JSON string
 */
public static String writeError(ErrorMessageMapper errorMessageMapper, APIError apiError, HttpHeaders httpHeaders) {
    JSONObjectBuilder jsonObjectBuilder = new JSONObjectBuilder();
    errorMessageMapper.onStart(jsonObjectBuilder, apiError, httpHeaders);
    Optional<String> optional = apiError.getDescription();
    optional.ifPresent(description -> errorMessageMapper.mapDescription(jsonObjectBuilder, description));
    errorMessageMapper.mapStatusCode(jsonObjectBuilder, apiError.getStatusCode());
    errorMessageMapper.mapTitle(jsonObjectBuilder, apiError.getTitle());
    errorMessageMapper.mapType(jsonObjectBuilder, apiError.getType());
    errorMessageMapper.onFinish(jsonObjectBuilder, apiError, httpHeaders);
    JsonObject jsonObject = jsonObjectBuilder.build();
    return jsonObject.toString();
}
Also used : JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) JsonObject(com.google.gson.JsonObject)

Example 7 with JSONObjectBuilder

use of com.liferay.apio.architect.message.json.JSONObjectBuilder in project com-liferay-apio-architect by liferay.

the class FormWriter method write.

/**
 * Writes the {@link Form} to a string.
 *
 * @return the JSON representation of the {@code Form}
 */
public String write() {
    JSONObjectBuilder jsonObjectBuilder = new JSONObjectBuilder();
    _formMessageMapper.onStart(jsonObjectBuilder, _form, _requestInfo.getHttpHeaders());
    String url = createFormURL(_requestInfo.getServerURL(), _form);
    _formMessageMapper.mapFormURL(jsonObjectBuilder, url);
    String title = _form.getTitle(_requestInfo.getLanguage());
    _formMessageMapper.mapFormTitle(jsonObjectBuilder, title);
    String description = _form.getDescription(_requestInfo.getLanguage());
    _formMessageMapper.mapFormDescription(jsonObjectBuilder, description);
    List<FormField> formFields = _form.getFormFields();
    formFields.forEach(formField -> _formMessageMapper.mapFormField(jsonObjectBuilder, formField));
    _formMessageMapper.onFinish(jsonObjectBuilder, _form, _requestInfo.getHttpHeaders());
    JsonObject jsonObject = jsonObjectBuilder.build();
    return jsonObject.toString();
}
Also used : JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) JsonObject(com.google.gson.JsonObject) FormField(com.liferay.apio.architect.form.FormField)

Aggregations

JSONObjectBuilder (com.liferay.apio.architect.message.json.JSONObjectBuilder)7 JsonObject (com.google.gson.JsonObject)5 WriterUtil.getFieldsWriter (com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter)4 Form (com.liferay.apio.architect.form.Form)3 Operation (com.liferay.apio.architect.operation.Operation)3 Optional (java.util.Optional)3 FunctionalList (com.liferay.apio.architect.list.FunctionalList)2 Representor (com.liferay.apio.architect.representor.Representor)2 RequestInfo (com.liferay.apio.architect.request.RequestInfo)2 SingleModel (com.liferay.apio.architect.single.model.SingleModel)2 Unsafe.unsafeCast (com.liferay.apio.architect.unsafe.Unsafe.unsafeCast)2 PathFunction (com.liferay.apio.architect.writer.alias.PathFunction)2 RepresentorFunction (com.liferay.apio.architect.writer.alias.RepresentorFunction)2 ResourceNameFunction (com.liferay.apio.architect.writer.alias.ResourceNameFunction)2 SingleModelFunction (com.liferay.apio.architect.writer.alias.SingleModelFunction)2 URLCreator.createFormURL (com.liferay.apio.architect.writer.url.URLCreator.createFormURL)2 WriterUtil.getPathOptional (com.liferay.apio.architect.writer.util.WriterUtil.getPathOptional)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2