/modules/apps/headless/headless-delivery/headless-delivery-client/src/main/java/com/liferay/headless/delivery/client/resource/v1_0/KnowledgeBaseAttachmentResource.java

https://github.com/kiyoshilee/liferay-portal · Java · 409 lines · 284 code · 108 blank · 17 comment · 17 complexity · 4b8390e75fc023d430735375b6c1221b MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.headless.delivery.client.resource.v1_0;
  15. import com.liferay.headless.delivery.client.dto.v1_0.KnowledgeBaseAttachment;
  16. import com.liferay.headless.delivery.client.http.HttpInvoker;
  17. import com.liferay.headless.delivery.client.pagination.Page;
  18. import com.liferay.headless.delivery.client.serdes.v1_0.KnowledgeBaseAttachmentSerDes;
  19. import java.io.File;
  20. import java.util.LinkedHashMap;
  21. import java.util.Locale;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javax.annotation.Generated;
  26. /**
  27. * @author Javier Gamarra
  28. * @generated
  29. */
  30. @Generated("")
  31. public interface KnowledgeBaseAttachmentResource {
  32. public static Builder builder() {
  33. return new Builder();
  34. }
  35. public Page<KnowledgeBaseAttachment>
  36. getKnowledgeBaseArticleKnowledgeBaseAttachmentsPage(
  37. Long knowledgeBaseArticleId)
  38. throws Exception;
  39. public HttpInvoker.HttpResponse
  40. getKnowledgeBaseArticleKnowledgeBaseAttachmentsPageHttpResponse(
  41. Long knowledgeBaseArticleId)
  42. throws Exception;
  43. public KnowledgeBaseAttachment
  44. postKnowledgeBaseArticleKnowledgeBaseAttachment(
  45. Long knowledgeBaseArticleId,
  46. KnowledgeBaseAttachment knowledgeBaseAttachment,
  47. Map<String, File> multipartFiles)
  48. throws Exception;
  49. public HttpInvoker.HttpResponse
  50. postKnowledgeBaseArticleKnowledgeBaseAttachmentHttpResponse(
  51. Long knowledgeBaseArticleId,
  52. KnowledgeBaseAttachment knowledgeBaseAttachment,
  53. Map<String, File> multipartFiles)
  54. throws Exception;
  55. public void deleteKnowledgeBaseAttachment(Long knowledgeBaseAttachmentId)
  56. throws Exception;
  57. public HttpInvoker.HttpResponse deleteKnowledgeBaseAttachmentHttpResponse(
  58. Long knowledgeBaseAttachmentId)
  59. throws Exception;
  60. public KnowledgeBaseAttachment getKnowledgeBaseAttachment(
  61. Long knowledgeBaseAttachmentId)
  62. throws Exception;
  63. public HttpInvoker.HttpResponse getKnowledgeBaseAttachmentHttpResponse(
  64. Long knowledgeBaseAttachmentId)
  65. throws Exception;
  66. public static class Builder {
  67. public Builder authentication(String login, String password) {
  68. _login = login;
  69. _password = password;
  70. return this;
  71. }
  72. public KnowledgeBaseAttachmentResource build() {
  73. return new KnowledgeBaseAttachmentResourceImpl(this);
  74. }
  75. public Builder endpoint(String host, int port, String scheme) {
  76. _host = host;
  77. _port = port;
  78. _scheme = scheme;
  79. return this;
  80. }
  81. public Builder header(String key, String value) {
  82. _headers.put(key, value);
  83. return this;
  84. }
  85. public Builder locale(Locale locale) {
  86. _locale = locale;
  87. return this;
  88. }
  89. public Builder parameter(String key, String value) {
  90. _parameters.put(key, value);
  91. return this;
  92. }
  93. private Builder() {
  94. }
  95. private Map<String, String> _headers = new LinkedHashMap<>();
  96. private String _host = "localhost";
  97. private Locale _locale;
  98. private String _login = "test@liferay.com";
  99. private String _password = "test";
  100. private Map<String, String> _parameters = new LinkedHashMap<>();
  101. private int _port = 8080;
  102. private String _scheme = "http";
  103. }
  104. public static class KnowledgeBaseAttachmentResourceImpl
  105. implements KnowledgeBaseAttachmentResource {
  106. public Page<KnowledgeBaseAttachment>
  107. getKnowledgeBaseArticleKnowledgeBaseAttachmentsPage(
  108. Long knowledgeBaseArticleId)
  109. throws Exception {
  110. HttpInvoker.HttpResponse httpResponse =
  111. getKnowledgeBaseArticleKnowledgeBaseAttachmentsPageHttpResponse(
  112. knowledgeBaseArticleId);
  113. String content = httpResponse.getContent();
  114. _logger.fine("HTTP response content: " + content);
  115. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  116. _logger.fine(
  117. "HTTP response status code: " + httpResponse.getStatusCode());
  118. return Page.of(content, KnowledgeBaseAttachmentSerDes::toDTO);
  119. }
  120. public HttpInvoker.HttpResponse
  121. getKnowledgeBaseArticleKnowledgeBaseAttachmentsPageHttpResponse(
  122. Long knowledgeBaseArticleId)
  123. throws Exception {
  124. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  125. if (_builder._locale != null) {
  126. httpInvoker.header(
  127. "Accept-Language", _builder._locale.toLanguageTag());
  128. }
  129. for (Map.Entry<String, String> entry :
  130. _builder._headers.entrySet()) {
  131. httpInvoker.header(entry.getKey(), entry.getValue());
  132. }
  133. for (Map.Entry<String, String> entry :
  134. _builder._parameters.entrySet()) {
  135. httpInvoker.parameter(entry.getKey(), entry.getValue());
  136. }
  137. httpInvoker.httpMethod(HttpInvoker.HttpMethod.GET);
  138. httpInvoker.path(
  139. _builder._scheme + "://" + _builder._host + ":" +
  140. _builder._port +
  141. "/o/headless-delivery/v1.0/knowledge-base-articles/{knowledgeBaseArticleId}/knowledge-base-attachments",
  142. knowledgeBaseArticleId);
  143. httpInvoker.userNameAndPassword(
  144. _builder._login + ":" + _builder._password);
  145. return httpInvoker.invoke();
  146. }
  147. public KnowledgeBaseAttachment
  148. postKnowledgeBaseArticleKnowledgeBaseAttachment(
  149. Long knowledgeBaseArticleId,
  150. KnowledgeBaseAttachment knowledgeBaseAttachment,
  151. Map<String, File> multipartFiles)
  152. throws Exception {
  153. HttpInvoker.HttpResponse httpResponse =
  154. postKnowledgeBaseArticleKnowledgeBaseAttachmentHttpResponse(
  155. knowledgeBaseArticleId, knowledgeBaseAttachment,
  156. multipartFiles);
  157. String content = httpResponse.getContent();
  158. _logger.fine("HTTP response content: " + content);
  159. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  160. _logger.fine(
  161. "HTTP response status code: " + httpResponse.getStatusCode());
  162. try {
  163. return KnowledgeBaseAttachmentSerDes.toDTO(content);
  164. }
  165. catch (Exception e) {
  166. _logger.log(
  167. Level.WARNING,
  168. "Unable to process HTTP response: " + content, e);
  169. throw e;
  170. }
  171. }
  172. public HttpInvoker.HttpResponse
  173. postKnowledgeBaseArticleKnowledgeBaseAttachmentHttpResponse(
  174. Long knowledgeBaseArticleId,
  175. KnowledgeBaseAttachment knowledgeBaseAttachment,
  176. Map<String, File> multipartFiles)
  177. throws Exception {
  178. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  179. httpInvoker.multipart();
  180. httpInvoker.part(
  181. "knowledgeBaseAttachment",
  182. KnowledgeBaseAttachmentSerDes.toJSON(knowledgeBaseAttachment));
  183. for (Map.Entry<String, File> entry : multipartFiles.entrySet()) {
  184. httpInvoker.part(entry.getKey(), entry.getValue());
  185. }
  186. if (_builder._locale != null) {
  187. httpInvoker.header(
  188. "Accept-Language", _builder._locale.toLanguageTag());
  189. }
  190. for (Map.Entry<String, String> entry :
  191. _builder._headers.entrySet()) {
  192. httpInvoker.header(entry.getKey(), entry.getValue());
  193. }
  194. for (Map.Entry<String, String> entry :
  195. _builder._parameters.entrySet()) {
  196. httpInvoker.parameter(entry.getKey(), entry.getValue());
  197. }
  198. httpInvoker.httpMethod(HttpInvoker.HttpMethod.POST);
  199. httpInvoker.path(
  200. _builder._scheme + "://" + _builder._host + ":" +
  201. _builder._port +
  202. "/o/headless-delivery/v1.0/knowledge-base-articles/{knowledgeBaseArticleId}/knowledge-base-attachments",
  203. knowledgeBaseArticleId);
  204. httpInvoker.userNameAndPassword(
  205. _builder._login + ":" + _builder._password);
  206. return httpInvoker.invoke();
  207. }
  208. public void deleteKnowledgeBaseAttachment(
  209. Long knowledgeBaseAttachmentId)
  210. throws Exception {
  211. HttpInvoker.HttpResponse httpResponse =
  212. deleteKnowledgeBaseAttachmentHttpResponse(
  213. knowledgeBaseAttachmentId);
  214. String content = httpResponse.getContent();
  215. _logger.fine("HTTP response content: " + content);
  216. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  217. _logger.fine(
  218. "HTTP response status code: " + httpResponse.getStatusCode());
  219. }
  220. public HttpInvoker.HttpResponse
  221. deleteKnowledgeBaseAttachmentHttpResponse(
  222. Long knowledgeBaseAttachmentId)
  223. throws Exception {
  224. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  225. if (_builder._locale != null) {
  226. httpInvoker.header(
  227. "Accept-Language", _builder._locale.toLanguageTag());
  228. }
  229. for (Map.Entry<String, String> entry :
  230. _builder._headers.entrySet()) {
  231. httpInvoker.header(entry.getKey(), entry.getValue());
  232. }
  233. for (Map.Entry<String, String> entry :
  234. _builder._parameters.entrySet()) {
  235. httpInvoker.parameter(entry.getKey(), entry.getValue());
  236. }
  237. httpInvoker.httpMethod(HttpInvoker.HttpMethod.DELETE);
  238. httpInvoker.path(
  239. _builder._scheme + "://" + _builder._host + ":" +
  240. _builder._port +
  241. "/o/headless-delivery/v1.0/knowledge-base-attachments/{knowledgeBaseAttachmentId}",
  242. knowledgeBaseAttachmentId);
  243. httpInvoker.userNameAndPassword(
  244. _builder._login + ":" + _builder._password);
  245. return httpInvoker.invoke();
  246. }
  247. public KnowledgeBaseAttachment getKnowledgeBaseAttachment(
  248. Long knowledgeBaseAttachmentId)
  249. throws Exception {
  250. HttpInvoker.HttpResponse httpResponse =
  251. getKnowledgeBaseAttachmentHttpResponse(
  252. knowledgeBaseAttachmentId);
  253. String content = httpResponse.getContent();
  254. _logger.fine("HTTP response content: " + content);
  255. _logger.fine("HTTP response message: " + httpResponse.getMessage());
  256. _logger.fine(
  257. "HTTP response status code: " + httpResponse.getStatusCode());
  258. try {
  259. return KnowledgeBaseAttachmentSerDes.toDTO(content);
  260. }
  261. catch (Exception e) {
  262. _logger.log(
  263. Level.WARNING,
  264. "Unable to process HTTP response: " + content, e);
  265. throw e;
  266. }
  267. }
  268. public HttpInvoker.HttpResponse getKnowledgeBaseAttachmentHttpResponse(
  269. Long knowledgeBaseAttachmentId)
  270. throws Exception {
  271. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  272. if (_builder._locale != null) {
  273. httpInvoker.header(
  274. "Accept-Language", _builder._locale.toLanguageTag());
  275. }
  276. for (Map.Entry<String, String> entry :
  277. _builder._headers.entrySet()) {
  278. httpInvoker.header(entry.getKey(), entry.getValue());
  279. }
  280. for (Map.Entry<String, String> entry :
  281. _builder._parameters.entrySet()) {
  282. httpInvoker.parameter(entry.getKey(), entry.getValue());
  283. }
  284. httpInvoker.httpMethod(HttpInvoker.HttpMethod.GET);
  285. httpInvoker.path(
  286. _builder._scheme + "://" + _builder._host + ":" +
  287. _builder._port +
  288. "/o/headless-delivery/v1.0/knowledge-base-attachments/{knowledgeBaseAttachmentId}",
  289. knowledgeBaseAttachmentId);
  290. httpInvoker.userNameAndPassword(
  291. _builder._login + ":" + _builder._password);
  292. return httpInvoker.invoke();
  293. }
  294. private KnowledgeBaseAttachmentResourceImpl(Builder builder) {
  295. _builder = builder;
  296. }
  297. private static final Logger _logger = Logger.getLogger(
  298. KnowledgeBaseAttachmentResource.class.getName());
  299. private Builder _builder;
  300. }
  301. }