diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/aggregation/Aggregation.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/aggregation/Aggregation.java new file mode 100644 index 00000000000000..c29c4991c8b716 --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/aggregation/Aggregation.java @@ -0,0 +1,30 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.aggregation; + +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@Generated("") +public class Aggregation { + + public Map getAggregationTerms() { + return _aggregationTerms; + } + + public void setAggregationTerms(Map aggregationTerms) { + _aggregationTerms = aggregationTerms; + } + + private Map _aggregationTerms = new HashMap<>(); + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/aggregation/Facet.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/aggregation/Facet.java new file mode 100644 index 00000000000000..6cd322d49d9926 --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/aggregation/Facet.java @@ -0,0 +1,70 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.aggregation; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@Generated("") +public class Facet { + + public Facet() { + } + + public Facet(String facetCriteria, List facetValues) { + _facetCriteria = facetCriteria; + _facetValues = facetValues; + } + + public String getFacetCriteria() { + return _facetCriteria; + } + + public List getFacetValues() { + return _facetValues; + } + + public void setFacetCriteria(String facetCriteria) { + _facetCriteria = facetCriteria; + } + + public void setFacetValues(List facetValues) { + _facetValues = facetValues; + } + + public static class FacetValue { + + public FacetValue() { + } + + public FacetValue(Integer numberOfOccurrences, String term) { + _numberOfOccurrences = numberOfOccurrences; + _term = term; + } + + public Integer getNumberOfOccurrences() { + return _numberOfOccurrences; + } + + public String getTerm() { + return _term; + } + + private Integer _numberOfOccurrences; + private String _term; + + } + + private String _facetCriteria; + private List _facetValues = new ArrayList<>(); + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/function/UnsafeSupplier.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/function/UnsafeSupplier.java new file mode 100644 index 00000000000000..08816addd66d38 --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/function/UnsafeSupplier.java @@ -0,0 +1,20 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.function; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@FunctionalInterface +@Generated("") +public interface UnsafeSupplier { + + public T get() throws E; + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/http/HttpInvoker.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/http/HttpInvoker.java new file mode 100644 index 00000000000000..abaf5684727ba9 --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/http/HttpInvoker.java @@ -0,0 +1,461 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.http; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; + +import java.lang.reflect.Field; + +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; + +import java.util.Base64; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@Generated("") +public class HttpInvoker { + + public static HttpInvoker newHttpInvoker() { + return new HttpInvoker(); + } + + public HttpInvoker body(String body, String contentType) { + _body = body; + _contentType = contentType; + + return this; + } + + public HttpInvoker header(String name, String value) { + _headers.put(name, value); + + return this; + } + + public HttpInvoker httpMethod(HttpMethod httpMethod) { + _httpMethod = httpMethod; + + return this; + } + + public HttpResponse invoke() throws IOException { + HttpResponse httpResponse = new HttpResponse(); + + HttpURLConnection httpURLConnection = _openHttpURLConnection(); + + byte[] binaryContent = _readResponse(httpURLConnection); + + httpResponse.setBinaryContent(binaryContent); + httpResponse.setContent(new String(binaryContent)); + + httpResponse.setContentType( + httpURLConnection.getHeaderField("Content-Type")); + httpResponse.setMessage(httpURLConnection.getResponseMessage()); + httpResponse.setStatusCode(httpURLConnection.getResponseCode()); + + httpURLConnection.disconnect(); + + return httpResponse; + } + + public HttpInvoker multipart() { + _contentType = + "multipart/form-data; charset=utf-8; boundary=__MULTIPART_BOUNDARY__"; + _multipartBoundary = "__MULTIPART_BOUNDARY__"; + + return this; + } + + public HttpInvoker parameter(String name, String value) { + return parameter(name, new String[] {value}); + } + + public HttpInvoker parameter(String name, String[] values) { + String[] oldValues = _parameters.get(name); + + if (oldValues != null) { + String[] newValues = new String[oldValues.length + values.length]; + + System.arraycopy(oldValues, 0, newValues, 0, oldValues.length); + System.arraycopy( + values, 0, newValues, oldValues.length, values.length); + + _parameters.put(name, newValues); + } + else { + _parameters.put(name, values); + } + + return this; + } + + public HttpInvoker part(String name, File file) { + _files.put(name, file); + + return this; + } + + public HttpInvoker part(String name, String value) { + _parts.put(name, value); + + return this; + } + + public HttpInvoker path(String path) { + _path = path; + + return this; + } + + public HttpInvoker path(String name, Object value) { + _path = _path.replaceFirst("\\{" + name + "\\}", String.valueOf(value)); + + return this; + } + + public HttpInvoker userNameAndPassword(String userNameAndPassword) + throws IOException { + + Base64.Encoder encoder = Base64.getEncoder(); + + _encodedUserNameAndPassword = new String( + encoder.encode(userNameAndPassword.getBytes("UTF-8")), "UTF-8"); + + return this; + } + + public enum HttpMethod { + + DELETE, GET, PATCH, POST, PUT + + } + + public class HttpResponse { + + public byte[] getBinaryContent() { + return _binaryContent; + } + + public String getContent() { + return _content; + } + + public String getContentType() { + return _contentType; + } + + public String getMessage() { + return _message; + } + + public int getStatusCode() { + return _statusCode; + } + + public void setBinaryContent(byte[] binaryContent) { + _binaryContent = binaryContent; + } + + public void setContent(String content) { + _content = content; + } + + public void setContentType(String contentType) { + _contentType = contentType; + } + + public void setMessage(String message) { + _message = message; + } + + public void setStatusCode(int statusCode) { + _statusCode = statusCode; + } + + private byte[] _binaryContent; + private String _content; + private String _contentType; + private String _message; + private int _statusCode; + + } + + private HttpInvoker() { + } + + private void _appendPart( + OutputStream outputStream, PrintWriter printWriter, String key, + Object value) + throws IOException { + + printWriter.append("\r\n--"); + printWriter.append(_multipartBoundary); + printWriter.append("\r\nContent-Disposition: form-data; name=\""); + printWriter.append(key); + printWriter.append("\";"); + + if (value instanceof File) { + File file = (File)value; + + printWriter.append(" filename=\""); + printWriter.append(_filter(file.getName())); + printWriter.append("\"\r\nContent-Type: "); + printWriter.append( + URLConnection.guessContentTypeFromName(file.getName())); + printWriter.append("\r\n\r\n"); + + printWriter.flush(); + + byte[] buffer = new byte[4096]; + FileInputStream fileInputStream = new FileInputStream(file); + int read = -1; + + while ((read = fileInputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, read); + } + + outputStream.flush(); + + fileInputStream.close(); + } + else { + printWriter.append("\r\n\r\n"); + printWriter.append(value.toString()); + } + + printWriter.append("\r\n"); + } + + private String _filter(String fileName) { + fileName = fileName.replaceAll("\"", ""); + fileName = fileName.replaceAll("\n", ""); + fileName = fileName.replaceAll("\r", ""); + + return fileName; + } + + private HttpURLConnection _getHttpURLConnection( + HttpMethod httpMethod, String urlString) + throws IOException { + + URL url = new URL(urlString); + + HttpURLConnection httpURLConnection = + (HttpURLConnection)url.openConnection(); + + try { + HttpURLConnection methodHttpURLConnection = httpURLConnection; + + if (Objects.equals(url.getProtocol(), "https")) { + Class clazz = httpURLConnection.getClass(); + + Field field = clazz.getDeclaredField("delegate"); + + field.setAccessible(true); + + methodHttpURLConnection = (HttpURLConnection)field.get( + httpURLConnection); + } + + _methodField.set(methodHttpURLConnection, httpMethod.name()); + } + catch (ReflectiveOperationException reflectiveOperationException) { + throw new IOException(reflectiveOperationException); + } + + return httpURLConnection; + } + + private String _getQueryString() throws IOException { + StringBuilder sb = new StringBuilder(); + + Set> set = _parameters.entrySet(); + + Iterator> iterator = set.iterator(); + + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + + String[] values = entry.getValue(); + + for (int i = 0; i < values.length; i++) { + String name = URLEncoder.encode(entry.getKey(), "UTF-8"); + + sb.append(name); + + sb.append("="); + + String value = URLEncoder.encode(values[i], "UTF-8"); + + sb.append(value); + + if ((i + 1) < values.length) { + sb.append("&"); + } + } + + if (iterator.hasNext()) { + sb.append("&"); + } + } + + return sb.toString(); + } + + private HttpURLConnection _openHttpURLConnection() throws IOException { + String urlString = _path; + + String queryString = _getQueryString(); + + if (queryString.length() > 0) { + if (!urlString.contains("?")) { + urlString += "?"; + } + + urlString += queryString; + } + + HttpURLConnection httpURLConnection = _getHttpURLConnection( + _httpMethod, urlString); + + if (_encodedUserNameAndPassword != null) { + httpURLConnection.setRequestProperty( + "Authorization", "Basic " + _encodedUserNameAndPassword); + } + + if (_contentType != null) { + httpURLConnection.setRequestProperty("Content-Type", _contentType); + } + + for (Map.Entry header : _headers.entrySet()) { + httpURLConnection.setRequestProperty( + header.getKey(), header.getValue()); + } + + _writeBody(httpURLConnection); + + return httpURLConnection; + } + + private byte[] _readResponse(HttpURLConnection httpURLConnection) + throws IOException { + + ByteArrayOutputStream byteArrayOutputStream = + new ByteArrayOutputStream(); + + InputStream inputStream = null; + + int responseCode = httpURLConnection.getResponseCode(); + + if (responseCode > 299) { + inputStream = httpURLConnection.getErrorStream(); + } + else { + inputStream = httpURLConnection.getInputStream(); + } + + byte[] bytes = new byte[8192]; + + while (true) { + int read = inputStream.read(bytes, 0, bytes.length); + + if (read == -1) { + break; + } + + byteArrayOutputStream.write(bytes, 0, read); + } + + byteArrayOutputStream.flush(); + + return byteArrayOutputStream.toByteArray(); + } + + private void _writeBody(HttpURLConnection httpURLConnection) + throws IOException { + + if ((_body == null) && _files.isEmpty() && _parts.isEmpty()) { + return; + } + + httpURLConnection.setDoOutput(true); + + OutputStream outputStream = httpURLConnection.getOutputStream(); + + try (PrintWriter printWriter = new PrintWriter( + new OutputStreamWriter(outputStream, "UTF-8"), true)) { + + if (_contentType.startsWith("multipart/form-data")) { + for (Map.Entry entry : _parts.entrySet()) { + _appendPart( + outputStream, printWriter, entry.getKey(), + entry.getValue()); + } + + for (Map.Entry entry : _files.entrySet()) { + _appendPart( + outputStream, printWriter, entry.getKey(), + entry.getValue()); + } + + printWriter.append("--" + _multipartBoundary + "--"); + + printWriter.flush(); + + outputStream.flush(); + } + else { + printWriter.append(_body); + + printWriter.flush(); + } + } + } + + private static final Field _methodField; + + static { + try { + _methodField = HttpURLConnection.class.getDeclaredField("method"); + + _methodField.setAccessible(true); + } + catch (Exception exception) { + throw new ExceptionInInitializerError(exception); + } + } + + private String _body; + private String _contentType; + private String _encodedUserNameAndPassword; + private final Map _files = new LinkedHashMap<>(); + private final Map _headers = new LinkedHashMap<>(); + private HttpMethod _httpMethod = HttpMethod.GET; + private String _multipartBoundary; + private final Map _parameters = new LinkedHashMap<>(); + private final Map _parts = new LinkedHashMap<>(); + private String _path; + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/json/BaseJSONParser.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/json/BaseJSONParser.java new file mode 100644 index 00000000000000..3bf90d77e26281 --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/json/BaseJSONParser.java @@ -0,0 +1,648 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.json; + +import java.math.BigDecimal; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Stack; +import java.util.TreeMap; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@Generated("") +public abstract class BaseJSONParser { + + public static final String[][] JSON_ESCAPE_STRINGS = new String[][] { + {"\\", "\\\\"}, {"\"", "\\\""}, {"\b", "\\b"}, {"\f", "\\f"}, + {"\n", "\\n"}, {"\r", "\\r"}, {"\t", "\\t"} + }; + + public T parseToDTO(String json) { + if (json == null) { + throw new IllegalArgumentException("JSON is null"); + } + + _init(json); + + _assertStartsWithAndEndsWith("{", "}"); + + T dto = createDTO(); + + if (_isEmpty()) { + return dto; + } + + _readNextChar(); + + _readWhileLastCharIsWhiteSpace(); + + _readNextChar(); + + if (_isLastChar('}')) { + _readWhileLastCharIsWhiteSpace(); + + if (!_isEndOfJSON()) { + _readNextChar(); + + throw new IllegalArgumentException( + "Expected end of JSON, but found '" + _lastChar + "'"); + } + + return dto; + } + + do { + _readWhileLastCharIsWhiteSpace(); + + String fieldName = _readValueAsString(); + + _readWhileLastCharIsWhiteSpace(); + + _assertLastChar(':'); + + _readNextChar(); + + _readWhileLastCharIsWhiteSpace(); + + setField(dto, fieldName, _readValue(parseMaps(fieldName))); + + _readWhileLastCharIsWhiteSpace(); + } + while (_ifLastCharMatchesThenRead(',')); + + return dto; + } + + public T[] parseToDTOs(String json) { + if (json == null) { + throw new IllegalArgumentException("JSON is null"); + } + + _init(json); + + _assertStartsWithAndEndsWith("[", "]"); + + if (_isEmpty()) { + return createDTOArray(0); + } + + _readNextChar(); + + _readWhileLastCharIsWhiteSpace(); + + if (_isLastChar(']')) { + _readNextChar(); + + return createDTOArray(0); + } + + _readWhileLastCharIsWhiteSpace(); + + Object[] objects = (Object[])_readValue(); + + T[] dtos = createDTOArray(objects.length); + + for (int i = 0; i < dtos.length; i++) { + dtos[i] = parseToDTO((String)objects[i]); + } + + return dtos; + } + + public Map parseToMap(String json) { + if (json == null) { + throw new IllegalArgumentException("JSON is null"); + } + + _init(json); + + _assertStartsWithAndEndsWith("{", "}"); + + Map map = new TreeMap<>(); + + _setCaptureStart(); + + _readNextChar(); + + _readNextChar(); + + _readWhileLastCharIsWhiteSpace(); + + if (_isLastChar('}')) { + return map; + } + + do { + _readWhileLastCharIsWhiteSpace(); + + String key = _readValueAsString(); + + _readWhileLastCharIsWhiteSpace(); + + if (!_ifLastCharMatchesThenRead(':')) { + throw new IllegalArgumentException("Expected ':'"); + } + + _readWhileLastCharIsWhiteSpace(); + + map.put(key, _readValue(true)); + + _readWhileLastCharIsWhiteSpace(); + } + while (_ifLastCharMatchesThenRead(',')); + + _readWhileLastCharIsWhiteSpace(); + + if (!_ifLastCharMatchesThenRead('}')) { + throw new IllegalArgumentException( + "Expected either ',' or '}', but found '" + _lastChar + "'"); + } + + return map; + } + + protected abstract T createDTO(); + + protected abstract T[] createDTOArray(int size); + + protected abstract boolean parseMaps(String jsonParserFieldName); + + protected abstract void setField( + T dto, String jsonParserFieldName, Object jsonParserFieldValue); + + protected BigDecimal[] toBigDecimals(Object[] objects) { + BigDecimal[] bigdecimals = new BigDecimal[objects.length]; + + for (int i = 0; i < bigdecimals.length; i++) { + bigdecimals[i] = new BigDecimal(objects[i].toString()); + } + + return bigdecimals; + } + + protected Date toDate(String string) { + try { + return _dateFormat.parse(string); + } + catch (ParseException pe) { + throw new IllegalArgumentException( + "Unable to parse date from " + string, pe); + } + } + + protected Date[] toDates(Object[] objects) { + Date[] dates = new Date[objects.length]; + + for (int i = 0; i < dates.length; i++) { + dates[i] = toDate((String)objects[i]); + } + + return dates; + } + + protected Integer[] toIntegers(Object[] objects) { + Integer[] integers = new Integer[objects.length]; + + for (int i = 0; i < integers.length; i++) { + integers[i] = Integer.valueOf(objects[i].toString()); + } + + return integers; + } + + protected Long[] toLongs(Object[] objects) { + Long[] longs = new Long[objects.length]; + + for (int i = 0; i < longs.length; i++) { + longs[i] = Long.valueOf(objects[i].toString()); + } + + return longs; + } + + protected String toString(Date date) { + return _dateFormat.format(date); + } + + protected String[] toStrings(Object[] objects) { + String[] strings = new String[objects.length]; + + for (int i = 0; i < strings.length; i++) { + strings[i] = (String)objects[i]; + } + + return strings; + } + + private void _assertLastChar(char c) { + if (_lastChar != c) { + throw new IllegalArgumentException( + String.format( + "Expected last char '%s', but found '%s'", c, _lastChar)); + } + } + + private void _assertStartsWithAndEndsWith(String prefix, String sufix) { + if (!_json.startsWith(prefix)) { + throw new IllegalArgumentException( + String.format( + "Expected starts with '%s', but found '%s' in '%s'", prefix, + _json.charAt(0), _json)); + } + + if (!_json.endsWith(sufix)) { + throw new IllegalArgumentException( + String.format( + "Expected ends with '%s', but found '%s' in '%s'", sufix, + _json.charAt(_json.length() - 1), _json)); + } + } + + private String _getCapturedJSONSubstring() { + return _json.substring(_captureStartStack.pop(), _index - 1); + } + + private String _getCapturedSubstring() { + return _unescape(_getCapturedJSONSubstring()); + } + + private boolean _ifLastCharMatchesThenRead(char ch) { + if (_lastChar != ch) { + return false; + } + + _readNextChar(); + + return true; + } + + private void _init(String json) { + _captureStartStack = new Stack<>(); + _dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXX"); + _index = 0; + _json = json.trim(); + _lastChar = 0; + } + + private boolean _isCharEscaped(String string, int index) { + int backslashCount = 0; + + while (((index - 1 - backslashCount) >= 0) && + (string.charAt(index - 1 - backslashCount) == '\\')) { + + backslashCount++; + } + + if ((backslashCount % 2) == 0) { + return false; + } + + return true; + } + + private boolean _isEmpty() { + String substring = _json.substring(1, _json.length() - 1); + + substring = substring.trim(); + + return substring.isEmpty(); + } + + private boolean _isEndOfJSON() { + if (_index == _json.length()) { + return true; + } + + return false; + } + + private boolean _isLastChar(char c) { + if (_lastChar == c) { + return true; + } + + return false; + } + + private boolean _isLastCharDecimalSeparator() { + if (_lastChar == '.') { + return true; + } + + return false; + } + + private boolean _isLastCharDigit() { + if ((_lastChar >= '0') && (_lastChar <= '9')) { + return true; + } + + return false; + } + + private boolean _isLastCharNegative() { + if (_lastChar == '-') { + return true; + } + + return false; + } + + private boolean _isLastCharPositive() { + if (_lastChar == '+') { + return true; + } + + return false; + } + + private boolean _isLastCharScientificNotation() { + if (_lastChar == 'E') { + return true; + } + + return false; + } + + private void _readNextChar() { + if (!_isEndOfJSON()) { + _lastChar = _json.charAt(_index++); + } + } + + private Object _readValue() { + return _readValue(false); + } + + private Object _readValue(boolean parseMaps) { + if (_lastChar == '[') { + return _readValueAsArray(parseMaps); + } + else if (_lastChar == 'f') { + return _readValueAsBooleanFalse(); + } + else if (_lastChar == 't') { + return _readValueAsBooleanTrue(); + } + else if (_lastChar == 'n') { + return _readValueAsObjectNull(); + } + else if (_lastChar == '"') { + return _readValueAsString(); + } + else if (parseMaps && (_lastChar == '{')) { + try { + Class clazz = getClass(); + + BaseJSONParser baseJSONParser = clazz.newInstance(); + + return baseJSONParser.parseToMap(_readValueAsStringJSON()); + } + catch (Exception e) { + throw new IllegalArgumentException( + "Expected JSON object or map"); + } + } + else if (_lastChar == '{') { + return _readValueAsStringJSON(); + } + else if ((_lastChar == '-') || (_lastChar == '0') || + (_lastChar == '1') || (_lastChar == '2') || + (_lastChar == '3') || (_lastChar == '4') || + (_lastChar == '5') || (_lastChar == '6') || + (_lastChar == '7') || (_lastChar == '8') || + (_lastChar == '9')) { + + return _readValueAsStringNumber(); + } + else { + throw new IllegalArgumentException(); + } + } + + private Object[] _readValueAsArray(boolean parseMaps) { + List objects = new ArrayList<>(); + + _readNextChar(); + + _readWhileLastCharIsWhiteSpace(); + + if (_isLastChar(']')) { + _readNextChar(); + + return objects.toArray(); + } + + do { + _readWhileLastCharIsWhiteSpace(); + + objects.add(_readValue(parseMaps)); + + _readWhileLastCharIsWhiteSpace(); + } + while (_ifLastCharMatchesThenRead(',')); + + if (!_isLastChar(']')) { + throw new IllegalArgumentException( + "Expected ']', but found '" + _lastChar + "'"); + } + + _readNextChar(); + + return objects.toArray(); + } + + private boolean _readValueAsBooleanFalse() { + _readNextChar(); + + _assertLastChar('a'); + + _readNextChar(); + + _assertLastChar('l'); + + _readNextChar(); + + _assertLastChar('s'); + + _readNextChar(); + + _assertLastChar('e'); + + _readNextChar(); + + return false; + } + + private boolean _readValueAsBooleanTrue() { + _readNextChar(); + + _assertLastChar('r'); + + _readNextChar(); + + _assertLastChar('u'); + + _readNextChar(); + + _assertLastChar('e'); + + _readNextChar(); + + return true; + } + + private Object _readValueAsObjectNull() { + _readNextChar(); + + _assertLastChar('u'); + + _readNextChar(); + + _assertLastChar('l'); + + _readNextChar(); + + _assertLastChar('l'); + + _readNextChar(); + + return null; + } + + private String _readValueAsString() { + _readNextChar(); + + _setCaptureStart(); + + while ((_lastChar != '"') || _isCharEscaped(_json, _index - 1)) { + _readNextChar(); + } + + String string = _getCapturedSubstring(); + + _readNextChar(); + + return string; + } + + private String _readValueAsStringJSON() { + _setCaptureStart(); + + _readNextChar(); + + if (_isLastChar('}')) { + _readNextChar(); + + return _getCapturedJSONSubstring(); + } + + _readWhileLastCharIsWhiteSpace(); + + if (_isLastChar('}')) { + _readNextChar(); + + return _getCapturedJSONSubstring(); + } + + do { + _readWhileLastCharIsWhiteSpace(); + + _readValueAsString(); + + _readWhileLastCharIsWhiteSpace(); + + if (!_ifLastCharMatchesThenRead(':')) { + throw new IllegalArgumentException("Expected ':'"); + } + + _readWhileLastCharIsWhiteSpace(); + + _readValue(); + + _readWhileLastCharIsWhiteSpace(); + } + while (_ifLastCharMatchesThenRead(',')); + + _readWhileLastCharIsWhiteSpace(); + + if (!_ifLastCharMatchesThenRead('}')) { + throw new IllegalArgumentException( + "Expected either ',' or '}', but found '" + _lastChar + "'"); + } + + return _getCapturedJSONSubstring(); + } + + private String _readValueAsStringNumber() { + _setCaptureStart(); + + do { + _readNextChar(); + } + while (_isLastCharDigit() || _isLastCharDecimalSeparator() || + _isLastCharNegative() || _isLastCharPositive() || + _isLastCharScientificNotation()); + + return _getCapturedSubstring(); + } + + private void _readWhileLastCharIsWhiteSpace() { + while ((_lastChar == ' ') || (_lastChar == '\n') || + (_lastChar == '\r') || (_lastChar == '\t')) { + + _readNextChar(); + } + } + + private void _setCaptureStart() { + _captureStartStack.push(_index - 1); + } + + private String _unescape(String string) { + for (int i = JSON_ESCAPE_STRINGS.length - 1; i >= 0; i--) { + String[] escapeStrings = JSON_ESCAPE_STRINGS[i]; + + int index = string.indexOf(escapeStrings[1]); + + while (index != -1) { + if (!_isCharEscaped(string, index)) { + string = + string.substring(0, index) + escapeStrings[0] + + string.substring(index + escapeStrings[1].length()); + + index = string.indexOf( + escapeStrings[1], index + escapeStrings[0].length()); + } + else { + index = string.indexOf( + escapeStrings[1], index + escapeStrings[1].length()); + } + } + } + + return string; + } + + private Stack _captureStartStack; + private DateFormat _dateFormat; + private int _index; + private String _json; + private char _lastChar; + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/pagination/Page.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/pagination/Page.java new file mode 100644 index 00000000000000..c89b377b77c63b --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/pagination/Page.java @@ -0,0 +1,326 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.pagination; + +import com.liferay.headless.admin.site.client.aggregation.Facet; +import com.liferay.headless.admin.site.client.json.BaseJSONParser; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Function; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@Generated("") +public class Page { + + public static Page of( + String json, Function toDTOFunction) { + + PageJSONParser pageJSONParser = new PageJSONParser(toDTOFunction); + + return (Page)pageJSONParser.parseToDTO(json); + } + + public T fetchFirstItem() { + Iterator iterator = _items.iterator(); + + if (iterator.hasNext()) { + return iterator.next(); + } + + return null; + } + + public Map> getActions() { + return _actions; + } + + public List getFacets() { + return _facets; + } + + public Collection getItems() { + return _items; + } + + public long getLastPage() { + if (_totalCount == 0) { + return 1; + } + + return -Math.floorDiv(-_totalCount, _pageSize); + } + + public long getPage() { + return _page; + } + + public long getPageSize() { + return _pageSize; + } + + public long getTotalCount() { + return _totalCount; + } + + public boolean hasNext() { + if (getLastPage() > _page) { + return true; + } + + return false; + } + + public boolean hasPrevious() { + if (_page > 1) { + return true; + } + + return false; + } + + public void setActions(Map> actions) { + _actions = actions; + } + + public void setFacets(List facets) { + _facets = facets; + } + + public void setItems(Collection items) { + _items = items; + } + + public void setPage(long page) { + _page = page; + } + + public void setPageSize(long pageSize) { + _pageSize = pageSize; + } + + public void setTotalCount(long totalCount) { + _totalCount = totalCount; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("{\"actions\": "); + + sb.append(_toString((Map)_actions)); + sb.append(", \"items\": ["); + + Iterator iterator = _items.iterator(); + + while (iterator.hasNext()) { + sb.append(iterator.next()); + + if (iterator.hasNext()) { + sb.append(", "); + } + } + + sb.append("], \"page\": "); + sb.append(_page); + sb.append(", \"pageSize\": "); + sb.append(_pageSize); + sb.append(", \"totalCount\": "); + sb.append(_totalCount); + sb.append("}"); + + return sb.toString(); + } + + public static class PageJSONParser extends BaseJSONParser { + + public PageJSONParser() { + _toDTOFunction = null; + } + + public PageJSONParser(Function toDTOFunction) { + _toDTOFunction = toDTOFunction; + } + + @Override + protected Page createDTO() { + return new Page(); + } + + @Override + protected Page[] createDTOArray(int size) { + return new Page[size]; + } + + @Override + protected boolean parseMaps(String jsonParserFieldName) { + if (Objects.equals(jsonParserFieldName, "actions")) { + return true; + } + else if (Objects.equals(jsonParserFieldName, "facets")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "items")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "lastPage")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "page")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "pageSize")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "totalCount")) { + return false; + } + else { + throw new IllegalArgumentException( + "Unsupported field name " + jsonParserFieldName); + } + } + + @Override + protected void setField( + Page page, String jsonParserFieldName, + Object jsonParserFieldValue) { + + if (Objects.equals(jsonParserFieldName, "actions")) { + if (jsonParserFieldValue != null) { + page.setActions( + (Map>)jsonParserFieldValue); + } + } + else if (Objects.equals(jsonParserFieldName, "facets")) { + if (jsonParserFieldValue == null) { + return; + } + + List facets = new ArrayList<>(); + + for (Object object1 : (Object[])jsonParserFieldValue) { + List facetValues = new ArrayList<>(); + + Map jsonParserFieldValuesMap = + this.parseToMap((String)object1); + + for (Object object2 : + (Object[])jsonParserFieldValuesMap.get( + "facetValues")) { + + Map facetValueMap = this.parseToMap( + (String)object2); + + facetValues.add( + new Facet.FacetValue( + Integer.valueOf( + (String)facetValueMap.get( + "numberOfOccurrences")), + (String)facetValueMap.get("term"))); + } + + facets.add( + new Facet( + (String)jsonParserFieldValuesMap.get( + "facetCriteria"), + facetValues)); + } + + page.setFacets(facets); + } + else if (Objects.equals(jsonParserFieldName, "items")) { + if (jsonParserFieldValue != null) { + List items = new ArrayList<>(); + + for (Object object : (Object[])jsonParserFieldValue) { + items.add(_toDTOFunction.apply((String)object)); + } + + page.setItems(items); + } + } + else if (Objects.equals(jsonParserFieldName, "lastPage")) { + } + else if (Objects.equals(jsonParserFieldName, "page")) { + if (jsonParserFieldValue != null) { + page.setPage(Long.valueOf((String)jsonParserFieldValue)); + } + } + else if (Objects.equals(jsonParserFieldName, "pageSize")) { + if (jsonParserFieldValue != null) { + page.setPageSize( + Long.valueOf((String)jsonParserFieldValue)); + } + } + else if (Objects.equals(jsonParserFieldName, "totalCount")) { + if (jsonParserFieldValue != null) { + page.setTotalCount( + Long.valueOf((String)jsonParserFieldValue)); + } + } + else { + throw new IllegalArgumentException( + "Unsupported field name " + jsonParserFieldName); + } + } + + private final Function _toDTOFunction; + + } + + private String _toString(Map map) { + StringBuilder sb = new StringBuilder("{"); + + Set> entries = map.entrySet(); + + Iterator> iterator = entries.iterator(); + + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + + sb.append("\""); + sb.append(entry.getKey()); + sb.append("\": "); + + Object value = entry.getValue(); + + if (value instanceof Map) { + sb.append(_toString((Map)value)); + } + else { + sb.append("\""); + sb.append(value); + sb.append("\""); + } + + if (iterator.hasNext()) { + sb.append(", "); + } + } + + sb.append("}"); + + return sb.toString(); + } + + private Map> _actions; + private List _facets = new ArrayList<>(); + private Collection _items; + private long _page; + private long _pageSize; + private long _totalCount; + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/pagination/Pagination.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/pagination/Pagination.java new file mode 100644 index 00000000000000..31534fb1a8f308 --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/pagination/Pagination.java @@ -0,0 +1,65 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.pagination; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@Generated("") +public class Pagination { + + public static Pagination of(int page, int pageSize) { + return new Pagination(page, pageSize); + } + + public int getEndPosition() { + if ((_page < 0) || (_pageSize < 0)) { + return -1; + } + + return _page * _pageSize; + } + + public int getPage() { + return _page; + } + + public int getPageSize() { + return _pageSize; + } + + public int getStartPosition() { + if ((_page < 0) || (_pageSize < 0)) { + return -1; + } + + return (_page - 1) * _pageSize; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("{\"page\": "); + + sb.append(_page); + sb.append(", \"pageSize\": "); + sb.append(_pageSize); + sb.append("}"); + + return sb.toString(); + } + + private Pagination(int page, int pageSize) { + _page = page; + _pageSize = pageSize; + } + + private final int _page; + private final int _pageSize; + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/permission/Permission.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/permission/Permission.java new file mode 100644 index 00000000000000..8c19b06797632b --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/permission/Permission.java @@ -0,0 +1,134 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.permission; + +import com.liferay.headless.admin.site.client.json.BaseJSONParser; + +import java.util.Objects; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@Generated("") +public class Permission { + + public static Permission toDTO(String json) { + PermissionJSONParser permissionJSONParser = + new PermissionJSONParser(); + + return permissionJSONParser.parseToDTO(json); + } + + public Object[] getActionIds() { + return actionIds; + } + + public String getRoleName() { + return roleName; + } + + public void setActionIds(Object[] actionIds) { + this.actionIds = actionIds; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("{"); + + if (actionIds != null) { + sb.append("\"actionIds\": ["); + + for (int i = 0; i < actionIds.length; i++) { + sb.append("\""); + sb.append(actionIds[i]); + sb.append("\""); + + if ((i + 1) < actionIds.length) { + sb.append(", "); + } + } + + sb.append("]"); + } + + if (roleName != null) { + if (sb.length() > 1) { + sb.append(", "); + } + + sb.append("\"roleName\": \""); + sb.append(roleName); + sb.append("\""); + } + + sb.append("}"); + + return sb.toString(); + } + + protected Object[] actionIds; + protected String roleName; + + private static class PermissionJSONParser + extends BaseJSONParser { + + @Override + protected Permission createDTO() { + return new Permission(); + } + + @Override + protected Permission[] createDTOArray(int size) { + return new Permission[size]; + } + + @Override + protected boolean parseMaps(String jsonParserFieldName) { + if (Objects.equals(jsonParserFieldName, "actionIds")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "roleName")) { + return false; + } + else { + throw new IllegalArgumentException( + "Unsupported field name " + jsonParserFieldName); + } + } + + @Override + protected void setField( + Permission permission, String jsonParserFieldName, + Object jsonParserFieldValue) { + + if (Objects.equals(jsonParserFieldName, "actionIds")) { + if (jsonParserFieldValue != null) { + permission.setActionIds((Object[])jsonParserFieldValue); + } + } + else if (Objects.equals(jsonParserFieldName, "roleName")) { + if (jsonParserFieldValue != null) { + permission.setRoleName((String)jsonParserFieldValue); + } + } + else { + throw new IllegalArgumentException( + "Unsupported field name " + jsonParserFieldName); + } + } + + } + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/problem/Problem.java b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/problem/Problem.java new file mode 100644 index 00000000000000..0bee4df0cbe4ae --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-client/src/main/java/com/liferay/headless/admin/site/client/problem/Problem.java @@ -0,0 +1,196 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.client.problem; + +import com.liferay.headless.admin.site.client.json.BaseJSONParser; + +import java.util.Objects; + +import javax.annotation.Generated; + +/** + * @author Rubén Pulido + * @generated + */ +@Generated("") +public class Problem { + + public static Problem toDTO(String json) { + ProblemJSONParser problemJSONParser = new ProblemJSONParser(); + + return problemJSONParser.parseToDTO(json); + } + + public static class ProblemException extends Exception { + + private Problem _problem; + + public Problem getProblem() { + return _problem; + } + + public ProblemException(Problem problem) { + super(problem.getTitle()); + + _problem = problem; + } + + } + + public String getDetail() { + return detail; + } + + public String getStatus() { + return status; + } + + public String getTitle() { + return title; + } + + public String getType() { + return type; + } + + public void setDetail(String detail) { + this.detail = detail; + } + + public void setStatus(String status) { + this.status = status; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("{"); + + if (detail != null) { + if (sb.length() > 1) { + sb.append(", "); + } + + sb.append("\"detail\": \""); + sb.append(detail); + sb.append("\""); + } + + if (status != null) { + if (sb.length() > 1) { + sb.append(", "); + } + + sb.append("\"status\": \""); + sb.append(status); + sb.append("\""); + } + + if (title != null) { + if (sb.length() > 1) { + sb.append(", "); + } + + sb.append("\"title\": \""); + sb.append(title); + sb.append("\""); + } + + if (type != null) { + if (sb.length() > 1) { + sb.append(", "); + } + + sb.append("\"type\": \""); + sb.append(type); + sb.append("\""); + } + + sb.append("}"); + + return sb.toString(); + } + + protected String detail; + protected String status; + protected String title; + protected String type; + + private static class ProblemJSONParser extends BaseJSONParser { + + @Override + protected Problem createDTO() { + return new Problem(); + } + + @Override + protected Problem[] createDTOArray(int size) { + return new Problem[size]; + } + + @Override + protected boolean parseMaps(String jsonParserFieldName) { + if (Objects.equals(jsonParserFieldName, "detail")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "status")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "title")) { + return false; + } + else if (Objects.equals(jsonParserFieldName, "type")) { + return false; + } + else { + throw new IllegalArgumentException( + "Unsupported field name " + jsonParserFieldName); + } + } + + @Override + protected void setField( + Problem problem, String jsonParserFieldName, + Object jsonParserFieldValue) { + + if (Objects.equals(jsonParserFieldName, "detail")) { + if (jsonParserFieldValue != null) { + problem.setDetail((String)jsonParserFieldValue); + } + } + else if (Objects.equals(jsonParserFieldName, "status")) { + if (jsonParserFieldValue != null) { + problem.setStatus((String)jsonParserFieldValue); + } + } + else if (Objects.equals(jsonParserFieldName, "title")) { + if (jsonParserFieldValue != null) { + problem.setTitle((String)jsonParserFieldValue); + } + } + else if (Objects.equals(jsonParserFieldName, "type")) { + if (jsonParserFieldValue != null) { + problem.setType((String)jsonParserFieldValue); + } + } + else { + throw new IllegalArgumentException( + "Unsupported field name " + jsonParserFieldName); + } + } + + } + +} \ No newline at end of file diff --git a/modules/apps/headless/headless-admin-site/headless-admin-site-impl/src/main/java/com/liferay/headless/admin/site/internal/jaxrs/application/HeadlessAdminSiteApplication.java b/modules/apps/headless/headless-admin-site/headless-admin-site-impl/src/main/java/com/liferay/headless/admin/site/internal/jaxrs/application/HeadlessAdminSiteApplication.java new file mode 100644 index 00000000000000..2e0c1f0e0b06a9 --- /dev/null +++ b/modules/apps/headless/headless-admin-site/headless-admin-site-impl/src/main/java/com/liferay/headless/admin/site/internal/jaxrs/application/HeadlessAdminSiteApplication.java @@ -0,0 +1,29 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.headless.admin.site.internal.jaxrs.application; + +import javax.annotation.Generated; + +import javax.ws.rs.core.Application; + +import org.osgi.service.component.annotations.Component; + +/** + * @author Rubén Pulido + * @generated + */ +@Component( + property = { + "liferay.jackson=false", + "osgi.jaxrs.application.base=/headless-admin-site", + "osgi.jaxrs.extension.select=(osgi.jaxrs.name=Liferay.Vulcan)", + "osgi.jaxrs.name=Liferay.Headless.Admin.Site" + }, + service = Application.class +) +@Generated("") +public class HeadlessAdminSiteApplication extends Application { +} \ No newline at end of file