/modules/apps/commerce/headless/headless-commerce/headless-commerce-delivery-cart-client/src/main/java/com/liferay/headless/commerce/delivery/cart/client/resource/v1_0/CartResource.java

https://github.com/danielreuther/liferay-portal · Java · 1079 lines · 801 code · 261 blank · 17 comment · 88 complexity · df5a3ba0f50e8100235398a213502c08 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.commerce.delivery.cart.client.resource.v1_0;
  15. import com.liferay.headless.commerce.delivery.cart.client.dto.v1_0.Cart;
  16. import com.liferay.headless.commerce.delivery.cart.client.dto.v1_0.CouponCode;
  17. import com.liferay.headless.commerce.delivery.cart.client.http.HttpInvoker;
  18. import com.liferay.headless.commerce.delivery.cart.client.pagination.Page;
  19. import com.liferay.headless.commerce.delivery.cart.client.pagination.Pagination;
  20. import com.liferay.headless.commerce.delivery.cart.client.problem.Problem;
  21. import com.liferay.headless.commerce.delivery.cart.client.serdes.v1_0.CartSerDes;
  22. import java.util.LinkedHashMap;
  23. import java.util.Locale;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javax.annotation.Generated;
  28. /**
  29. * @author Andrea Sbarra
  30. * @generated
  31. */
  32. @Generated("")
  33. public interface CartResource {
  34. public static Builder builder() {
  35. return new Builder();
  36. }
  37. public void deleteCart(Long cartId) throws Exception;
  38. public HttpInvoker.HttpResponse deleteCartHttpResponse(Long cartId)
  39. throws Exception;
  40. public void deleteCartBatch(String callbackURL, Object object)
  41. throws Exception;
  42. public HttpInvoker.HttpResponse deleteCartBatchHttpResponse(
  43. String callbackURL, Object object)
  44. throws Exception;
  45. public Cart getCart(Long cartId) throws Exception;
  46. public HttpInvoker.HttpResponse getCartHttpResponse(Long cartId)
  47. throws Exception;
  48. public Cart patchCart(Long cartId, Cart cart) throws Exception;
  49. public HttpInvoker.HttpResponse patchCartHttpResponse(
  50. Long cartId, Cart cart)
  51. throws Exception;
  52. public Cart putCart(Long cartId, Cart cart) throws Exception;
  53. public HttpInvoker.HttpResponse putCartHttpResponse(Long cartId, Cart cart)
  54. throws Exception;
  55. public void putCartBatch(String callbackURL, Object object)
  56. throws Exception;
  57. public HttpInvoker.HttpResponse putCartBatchHttpResponse(
  58. String callbackURL, Object object)
  59. throws Exception;
  60. public Cart postCartCheckout(Long cartId) throws Exception;
  61. public HttpInvoker.HttpResponse postCartCheckoutHttpResponse(Long cartId)
  62. throws Exception;
  63. public Cart postCartCouponCode(Long cartId, CouponCode couponCode)
  64. throws Exception;
  65. public HttpInvoker.HttpResponse postCartCouponCodeHttpResponse(
  66. Long cartId, CouponCode couponCode)
  67. throws Exception;
  68. public String getCartPaymentURL(Long cartId, String callbackURL)
  69. throws Exception;
  70. public HttpInvoker.HttpResponse getCartPaymentURLHttpResponse(
  71. Long cartId, String callbackURL)
  72. throws Exception;
  73. public Page<Cart> getChannelCartsPage(
  74. Long accountId, Long channelId, Pagination pagination)
  75. throws Exception;
  76. public HttpInvoker.HttpResponse getChannelCartsPageHttpResponse(
  77. Long accountId, Long channelId, Pagination pagination)
  78. throws Exception;
  79. public Cart postChannelCart(Long channelId, Cart cart) throws Exception;
  80. public HttpInvoker.HttpResponse postChannelCartHttpResponse(
  81. Long channelId, Cart cart)
  82. throws Exception;
  83. public static class Builder {
  84. public Builder authentication(String login, String password) {
  85. _login = login;
  86. _password = password;
  87. return this;
  88. }
  89. public CartResource build() {
  90. return new CartResourceImpl(this);
  91. }
  92. public Builder contextPath(String contextPath) {
  93. _contextPath = contextPath;
  94. return this;
  95. }
  96. public Builder endpoint(String host, int port, String scheme) {
  97. _host = host;
  98. _port = port;
  99. _scheme = scheme;
  100. return this;
  101. }
  102. public Builder header(String key, String value) {
  103. _headers.put(key, value);
  104. return this;
  105. }
  106. public Builder locale(Locale locale) {
  107. _locale = locale;
  108. return this;
  109. }
  110. public Builder parameter(String key, String value) {
  111. _parameters.put(key, value);
  112. return this;
  113. }
  114. public Builder parameters(String... parameters) {
  115. if ((parameters.length % 2) != 0) {
  116. throw new IllegalArgumentException(
  117. "Parameters length is not an even number");
  118. }
  119. for (int i = 0; i < parameters.length; i += 2) {
  120. String parameterName = String.valueOf(parameters[i]);
  121. String parameterValue = String.valueOf(parameters[i + 1]);
  122. _parameters.put(parameterName, parameterValue);
  123. }
  124. return this;
  125. }
  126. private Builder() {
  127. }
  128. private String _contextPath = "";
  129. private Map<String, String> _headers = new LinkedHashMap<>();
  130. private String _host = "localhost";
  131. private Locale _locale;
  132. private String _login = "";
  133. private String _password = "";
  134. private Map<String, String> _parameters = new LinkedHashMap<>();
  135. private int _port = 8080;
  136. private String _scheme = "http";
  137. }
  138. public static class CartResourceImpl implements CartResource {
  139. public void deleteCart(Long cartId) throws Exception {
  140. HttpInvoker.HttpResponse httpResponse = deleteCartHttpResponse(
  141. cartId);
  142. String content = httpResponse.getContent();
  143. if ((httpResponse.getStatusCode() / 100) != 2) {
  144. _logger.log(
  145. Level.WARNING,
  146. "Unable to process HTTP response content: " + content);
  147. _logger.log(
  148. Level.WARNING,
  149. "HTTP response message: " + httpResponse.getMessage());
  150. _logger.log(
  151. Level.WARNING,
  152. "HTTP response status code: " +
  153. httpResponse.getStatusCode());
  154. throw new Problem.ProblemException(Problem.toDTO(content));
  155. }
  156. else {
  157. _logger.fine("HTTP response content: " + content);
  158. _logger.fine(
  159. "HTTP response message: " + httpResponse.getMessage());
  160. _logger.fine(
  161. "HTTP response status code: " +
  162. httpResponse.getStatusCode());
  163. }
  164. }
  165. public HttpInvoker.HttpResponse deleteCartHttpResponse(Long cartId)
  166. throws Exception {
  167. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  168. if (_builder._locale != null) {
  169. httpInvoker.header(
  170. "Accept-Language", _builder._locale.toLanguageTag());
  171. }
  172. for (Map.Entry<String, String> entry :
  173. _builder._headers.entrySet()) {
  174. httpInvoker.header(entry.getKey(), entry.getValue());
  175. }
  176. for (Map.Entry<String, String> entry :
  177. _builder._parameters.entrySet()) {
  178. httpInvoker.parameter(entry.getKey(), entry.getValue());
  179. }
  180. httpInvoker.httpMethod(HttpInvoker.HttpMethod.DELETE);
  181. httpInvoker.path(
  182. _builder._scheme + "://" + _builder._host + ":" +
  183. _builder._port + _builder._contextPath +
  184. "/o/headless-commerce-delivery-cart/v1.0/carts/{cartId}");
  185. httpInvoker.path("cartId", cartId);
  186. httpInvoker.userNameAndPassword(
  187. _builder._login + ":" + _builder._password);
  188. return httpInvoker.invoke();
  189. }
  190. public void deleteCartBatch(String callbackURL, Object object)
  191. throws Exception {
  192. HttpInvoker.HttpResponse httpResponse = deleteCartBatchHttpResponse(
  193. callbackURL, object);
  194. String content = httpResponse.getContent();
  195. if ((httpResponse.getStatusCode() / 100) != 2) {
  196. _logger.log(
  197. Level.WARNING,
  198. "Unable to process HTTP response content: " + content);
  199. _logger.log(
  200. Level.WARNING,
  201. "HTTP response message: " + httpResponse.getMessage());
  202. _logger.log(
  203. Level.WARNING,
  204. "HTTP response status code: " +
  205. httpResponse.getStatusCode());
  206. throw new Problem.ProblemException(Problem.toDTO(content));
  207. }
  208. else {
  209. _logger.fine("HTTP response content: " + content);
  210. _logger.fine(
  211. "HTTP response message: " + httpResponse.getMessage());
  212. _logger.fine(
  213. "HTTP response status code: " +
  214. httpResponse.getStatusCode());
  215. }
  216. }
  217. public HttpInvoker.HttpResponse deleteCartBatchHttpResponse(
  218. String callbackURL, Object object)
  219. throws Exception {
  220. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  221. httpInvoker.body(object.toString(), "application/json");
  222. if (_builder._locale != null) {
  223. httpInvoker.header(
  224. "Accept-Language", _builder._locale.toLanguageTag());
  225. }
  226. for (Map.Entry<String, String> entry :
  227. _builder._headers.entrySet()) {
  228. httpInvoker.header(entry.getKey(), entry.getValue());
  229. }
  230. for (Map.Entry<String, String> entry :
  231. _builder._parameters.entrySet()) {
  232. httpInvoker.parameter(entry.getKey(), entry.getValue());
  233. }
  234. httpInvoker.httpMethod(HttpInvoker.HttpMethod.DELETE);
  235. if (callbackURL != null) {
  236. httpInvoker.parameter(
  237. "callbackURL", String.valueOf(callbackURL));
  238. }
  239. httpInvoker.path(
  240. _builder._scheme + "://" + _builder._host + ":" +
  241. _builder._port + _builder._contextPath +
  242. "/o/headless-commerce-delivery-cart/v1.0/carts/batch");
  243. httpInvoker.userNameAndPassword(
  244. _builder._login + ":" + _builder._password);
  245. return httpInvoker.invoke();
  246. }
  247. public Cart getCart(Long cartId) throws Exception {
  248. HttpInvoker.HttpResponse httpResponse = getCartHttpResponse(cartId);
  249. String content = httpResponse.getContent();
  250. if ((httpResponse.getStatusCode() / 100) != 2) {
  251. _logger.log(
  252. Level.WARNING,
  253. "Unable to process HTTP response content: " + content);
  254. _logger.log(
  255. Level.WARNING,
  256. "HTTP response message: " + httpResponse.getMessage());
  257. _logger.log(
  258. Level.WARNING,
  259. "HTTP response status code: " +
  260. httpResponse.getStatusCode());
  261. throw new Problem.ProblemException(Problem.toDTO(content));
  262. }
  263. else {
  264. _logger.fine("HTTP response content: " + content);
  265. _logger.fine(
  266. "HTTP response message: " + httpResponse.getMessage());
  267. _logger.fine(
  268. "HTTP response status code: " +
  269. httpResponse.getStatusCode());
  270. }
  271. try {
  272. return CartSerDes.toDTO(content);
  273. }
  274. catch (Exception e) {
  275. _logger.log(
  276. Level.WARNING,
  277. "Unable to process HTTP response: " + content, e);
  278. throw new Problem.ProblemException(Problem.toDTO(content));
  279. }
  280. }
  281. public HttpInvoker.HttpResponse getCartHttpResponse(Long cartId)
  282. throws Exception {
  283. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  284. if (_builder._locale != null) {
  285. httpInvoker.header(
  286. "Accept-Language", _builder._locale.toLanguageTag());
  287. }
  288. for (Map.Entry<String, String> entry :
  289. _builder._headers.entrySet()) {
  290. httpInvoker.header(entry.getKey(), entry.getValue());
  291. }
  292. for (Map.Entry<String, String> entry :
  293. _builder._parameters.entrySet()) {
  294. httpInvoker.parameter(entry.getKey(), entry.getValue());
  295. }
  296. httpInvoker.httpMethod(HttpInvoker.HttpMethod.GET);
  297. httpInvoker.path(
  298. _builder._scheme + "://" + _builder._host + ":" +
  299. _builder._port + _builder._contextPath +
  300. "/o/headless-commerce-delivery-cart/v1.0/carts/{cartId}");
  301. httpInvoker.path("cartId", cartId);
  302. httpInvoker.userNameAndPassword(
  303. _builder._login + ":" + _builder._password);
  304. return httpInvoker.invoke();
  305. }
  306. public Cart patchCart(Long cartId, Cart cart) throws Exception {
  307. HttpInvoker.HttpResponse httpResponse = patchCartHttpResponse(
  308. cartId, cart);
  309. String content = httpResponse.getContent();
  310. if ((httpResponse.getStatusCode() / 100) != 2) {
  311. _logger.log(
  312. Level.WARNING,
  313. "Unable to process HTTP response content: " + content);
  314. _logger.log(
  315. Level.WARNING,
  316. "HTTP response message: " + httpResponse.getMessage());
  317. _logger.log(
  318. Level.WARNING,
  319. "HTTP response status code: " +
  320. httpResponse.getStatusCode());
  321. throw new Problem.ProblemException(Problem.toDTO(content));
  322. }
  323. else {
  324. _logger.fine("HTTP response content: " + content);
  325. _logger.fine(
  326. "HTTP response message: " + httpResponse.getMessage());
  327. _logger.fine(
  328. "HTTP response status code: " +
  329. httpResponse.getStatusCode());
  330. }
  331. try {
  332. return CartSerDes.toDTO(content);
  333. }
  334. catch (Exception e) {
  335. _logger.log(
  336. Level.WARNING,
  337. "Unable to process HTTP response: " + content, e);
  338. throw new Problem.ProblemException(Problem.toDTO(content));
  339. }
  340. }
  341. public HttpInvoker.HttpResponse patchCartHttpResponse(
  342. Long cartId, Cart cart)
  343. throws Exception {
  344. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  345. httpInvoker.body(cart.toString(), "application/json");
  346. if (_builder._locale != null) {
  347. httpInvoker.header(
  348. "Accept-Language", _builder._locale.toLanguageTag());
  349. }
  350. for (Map.Entry<String, String> entry :
  351. _builder._headers.entrySet()) {
  352. httpInvoker.header(entry.getKey(), entry.getValue());
  353. }
  354. for (Map.Entry<String, String> entry :
  355. _builder._parameters.entrySet()) {
  356. httpInvoker.parameter(entry.getKey(), entry.getValue());
  357. }
  358. httpInvoker.httpMethod(HttpInvoker.HttpMethod.PATCH);
  359. httpInvoker.path(
  360. _builder._scheme + "://" + _builder._host + ":" +
  361. _builder._port + _builder._contextPath +
  362. "/o/headless-commerce-delivery-cart/v1.0/carts/{cartId}");
  363. httpInvoker.path("cartId", cartId);
  364. httpInvoker.userNameAndPassword(
  365. _builder._login + ":" + _builder._password);
  366. return httpInvoker.invoke();
  367. }
  368. public Cart putCart(Long cartId, Cart cart) throws Exception {
  369. HttpInvoker.HttpResponse httpResponse = putCartHttpResponse(
  370. cartId, cart);
  371. String content = httpResponse.getContent();
  372. if ((httpResponse.getStatusCode() / 100) != 2) {
  373. _logger.log(
  374. Level.WARNING,
  375. "Unable to process HTTP response content: " + content);
  376. _logger.log(
  377. Level.WARNING,
  378. "HTTP response message: " + httpResponse.getMessage());
  379. _logger.log(
  380. Level.WARNING,
  381. "HTTP response status code: " +
  382. httpResponse.getStatusCode());
  383. throw new Problem.ProblemException(Problem.toDTO(content));
  384. }
  385. else {
  386. _logger.fine("HTTP response content: " + content);
  387. _logger.fine(
  388. "HTTP response message: " + httpResponse.getMessage());
  389. _logger.fine(
  390. "HTTP response status code: " +
  391. httpResponse.getStatusCode());
  392. }
  393. try {
  394. return CartSerDes.toDTO(content);
  395. }
  396. catch (Exception e) {
  397. _logger.log(
  398. Level.WARNING,
  399. "Unable to process HTTP response: " + content, e);
  400. throw new Problem.ProblemException(Problem.toDTO(content));
  401. }
  402. }
  403. public HttpInvoker.HttpResponse putCartHttpResponse(
  404. Long cartId, Cart cart)
  405. throws Exception {
  406. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  407. httpInvoker.body(cart.toString(), "application/json");
  408. if (_builder._locale != null) {
  409. httpInvoker.header(
  410. "Accept-Language", _builder._locale.toLanguageTag());
  411. }
  412. for (Map.Entry<String, String> entry :
  413. _builder._headers.entrySet()) {
  414. httpInvoker.header(entry.getKey(), entry.getValue());
  415. }
  416. for (Map.Entry<String, String> entry :
  417. _builder._parameters.entrySet()) {
  418. httpInvoker.parameter(entry.getKey(), entry.getValue());
  419. }
  420. httpInvoker.httpMethod(HttpInvoker.HttpMethod.PUT);
  421. httpInvoker.path(
  422. _builder._scheme + "://" + _builder._host + ":" +
  423. _builder._port + _builder._contextPath +
  424. "/o/headless-commerce-delivery-cart/v1.0/carts/{cartId}");
  425. httpInvoker.path("cartId", cartId);
  426. httpInvoker.userNameAndPassword(
  427. _builder._login + ":" + _builder._password);
  428. return httpInvoker.invoke();
  429. }
  430. public void putCartBatch(String callbackURL, Object object)
  431. throws Exception {
  432. HttpInvoker.HttpResponse httpResponse = putCartBatchHttpResponse(
  433. callbackURL, object);
  434. String content = httpResponse.getContent();
  435. if ((httpResponse.getStatusCode() / 100) != 2) {
  436. _logger.log(
  437. Level.WARNING,
  438. "Unable to process HTTP response content: " + content);
  439. _logger.log(
  440. Level.WARNING,
  441. "HTTP response message: " + httpResponse.getMessage());
  442. _logger.log(
  443. Level.WARNING,
  444. "HTTP response status code: " +
  445. httpResponse.getStatusCode());
  446. throw new Problem.ProblemException(Problem.toDTO(content));
  447. }
  448. else {
  449. _logger.fine("HTTP response content: " + content);
  450. _logger.fine(
  451. "HTTP response message: " + httpResponse.getMessage());
  452. _logger.fine(
  453. "HTTP response status code: " +
  454. httpResponse.getStatusCode());
  455. }
  456. }
  457. public HttpInvoker.HttpResponse putCartBatchHttpResponse(
  458. String callbackURL, Object object)
  459. throws Exception {
  460. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  461. httpInvoker.body(object.toString(), "application/json");
  462. if (_builder._locale != null) {
  463. httpInvoker.header(
  464. "Accept-Language", _builder._locale.toLanguageTag());
  465. }
  466. for (Map.Entry<String, String> entry :
  467. _builder._headers.entrySet()) {
  468. httpInvoker.header(entry.getKey(), entry.getValue());
  469. }
  470. for (Map.Entry<String, String> entry :
  471. _builder._parameters.entrySet()) {
  472. httpInvoker.parameter(entry.getKey(), entry.getValue());
  473. }
  474. httpInvoker.httpMethod(HttpInvoker.HttpMethod.PUT);
  475. if (callbackURL != null) {
  476. httpInvoker.parameter(
  477. "callbackURL", String.valueOf(callbackURL));
  478. }
  479. httpInvoker.path(
  480. _builder._scheme + "://" + _builder._host + ":" +
  481. _builder._port + _builder._contextPath +
  482. "/o/headless-commerce-delivery-cart/v1.0/carts/batch");
  483. httpInvoker.userNameAndPassword(
  484. _builder._login + ":" + _builder._password);
  485. return httpInvoker.invoke();
  486. }
  487. public Cart postCartCheckout(Long cartId) throws Exception {
  488. HttpInvoker.HttpResponse httpResponse =
  489. postCartCheckoutHttpResponse(cartId);
  490. String content = httpResponse.getContent();
  491. if ((httpResponse.getStatusCode() / 100) != 2) {
  492. _logger.log(
  493. Level.WARNING,
  494. "Unable to process HTTP response content: " + content);
  495. _logger.log(
  496. Level.WARNING,
  497. "HTTP response message: " + httpResponse.getMessage());
  498. _logger.log(
  499. Level.WARNING,
  500. "HTTP response status code: " +
  501. httpResponse.getStatusCode());
  502. throw new Problem.ProblemException(Problem.toDTO(content));
  503. }
  504. else {
  505. _logger.fine("HTTP response content: " + content);
  506. _logger.fine(
  507. "HTTP response message: " + httpResponse.getMessage());
  508. _logger.fine(
  509. "HTTP response status code: " +
  510. httpResponse.getStatusCode());
  511. }
  512. try {
  513. return CartSerDes.toDTO(content);
  514. }
  515. catch (Exception e) {
  516. _logger.log(
  517. Level.WARNING,
  518. "Unable to process HTTP response: " + content, e);
  519. throw new Problem.ProblemException(Problem.toDTO(content));
  520. }
  521. }
  522. public HttpInvoker.HttpResponse postCartCheckoutHttpResponse(
  523. Long cartId)
  524. throws Exception {
  525. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  526. if (_builder._locale != null) {
  527. httpInvoker.header(
  528. "Accept-Language", _builder._locale.toLanguageTag());
  529. }
  530. for (Map.Entry<String, String> entry :
  531. _builder._headers.entrySet()) {
  532. httpInvoker.header(entry.getKey(), entry.getValue());
  533. }
  534. for (Map.Entry<String, String> entry :
  535. _builder._parameters.entrySet()) {
  536. httpInvoker.parameter(entry.getKey(), entry.getValue());
  537. }
  538. httpInvoker.httpMethod(HttpInvoker.HttpMethod.POST);
  539. httpInvoker.path(
  540. _builder._scheme + "://" + _builder._host + ":" +
  541. _builder._port + _builder._contextPath +
  542. "/o/headless-commerce-delivery-cart/v1.0/carts/{cartId}/checkout");
  543. httpInvoker.path("cartId", cartId);
  544. httpInvoker.userNameAndPassword(
  545. _builder._login + ":" + _builder._password);
  546. return httpInvoker.invoke();
  547. }
  548. public Cart postCartCouponCode(Long cartId, CouponCode couponCode)
  549. throws Exception {
  550. HttpInvoker.HttpResponse httpResponse =
  551. postCartCouponCodeHttpResponse(cartId, couponCode);
  552. String content = httpResponse.getContent();
  553. if ((httpResponse.getStatusCode() / 100) != 2) {
  554. _logger.log(
  555. Level.WARNING,
  556. "Unable to process HTTP response content: " + content);
  557. _logger.log(
  558. Level.WARNING,
  559. "HTTP response message: " + httpResponse.getMessage());
  560. _logger.log(
  561. Level.WARNING,
  562. "HTTP response status code: " +
  563. httpResponse.getStatusCode());
  564. throw new Problem.ProblemException(Problem.toDTO(content));
  565. }
  566. else {
  567. _logger.fine("HTTP response content: " + content);
  568. _logger.fine(
  569. "HTTP response message: " + httpResponse.getMessage());
  570. _logger.fine(
  571. "HTTP response status code: " +
  572. httpResponse.getStatusCode());
  573. }
  574. try {
  575. return CartSerDes.toDTO(content);
  576. }
  577. catch (Exception e) {
  578. _logger.log(
  579. Level.WARNING,
  580. "Unable to process HTTP response: " + content, e);
  581. throw new Problem.ProblemException(Problem.toDTO(content));
  582. }
  583. }
  584. public HttpInvoker.HttpResponse postCartCouponCodeHttpResponse(
  585. Long cartId, CouponCode couponCode)
  586. throws Exception {
  587. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  588. httpInvoker.body(couponCode.toString(), "application/json");
  589. if (_builder._locale != null) {
  590. httpInvoker.header(
  591. "Accept-Language", _builder._locale.toLanguageTag());
  592. }
  593. for (Map.Entry<String, String> entry :
  594. _builder._headers.entrySet()) {
  595. httpInvoker.header(entry.getKey(), entry.getValue());
  596. }
  597. for (Map.Entry<String, String> entry :
  598. _builder._parameters.entrySet()) {
  599. httpInvoker.parameter(entry.getKey(), entry.getValue());
  600. }
  601. httpInvoker.httpMethod(HttpInvoker.HttpMethod.POST);
  602. httpInvoker.path(
  603. _builder._scheme + "://" + _builder._host + ":" +
  604. _builder._port + _builder._contextPath +
  605. "/o/headless-commerce-delivery-cart/v1.0/carts/{cartId}/coupon-code");
  606. httpInvoker.path("cartId", cartId);
  607. httpInvoker.userNameAndPassword(
  608. _builder._login + ":" + _builder._password);
  609. return httpInvoker.invoke();
  610. }
  611. public String getCartPaymentURL(Long cartId, String callbackURL)
  612. throws Exception {
  613. HttpInvoker.HttpResponse httpResponse =
  614. getCartPaymentURLHttpResponse(cartId, callbackURL);
  615. String content = httpResponse.getContent();
  616. if ((httpResponse.getStatusCode() / 100) != 2) {
  617. _logger.log(
  618. Level.WARNING,
  619. "Unable to process HTTP response content: " + content);
  620. _logger.log(
  621. Level.WARNING,
  622. "HTTP response message: " + httpResponse.getMessage());
  623. _logger.log(
  624. Level.WARNING,
  625. "HTTP response status code: " +
  626. httpResponse.getStatusCode());
  627. throw new Problem.ProblemException(Problem.toDTO(content));
  628. }
  629. else {
  630. _logger.fine("HTTP response content: " + content);
  631. _logger.fine(
  632. "HTTP response message: " + httpResponse.getMessage());
  633. _logger.fine(
  634. "HTTP response status code: " +
  635. httpResponse.getStatusCode());
  636. }
  637. try {
  638. return content;
  639. }
  640. catch (Exception e) {
  641. _logger.log(
  642. Level.WARNING,
  643. "Unable to process HTTP response: " + content, e);
  644. throw new Problem.ProblemException(Problem.toDTO(content));
  645. }
  646. }
  647. public HttpInvoker.HttpResponse getCartPaymentURLHttpResponse(
  648. Long cartId, String callbackURL)
  649. throws Exception {
  650. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  651. if (_builder._locale != null) {
  652. httpInvoker.header(
  653. "Accept-Language", _builder._locale.toLanguageTag());
  654. }
  655. for (Map.Entry<String, String> entry :
  656. _builder._headers.entrySet()) {
  657. httpInvoker.header(entry.getKey(), entry.getValue());
  658. }
  659. for (Map.Entry<String, String> entry :
  660. _builder._parameters.entrySet()) {
  661. httpInvoker.parameter(entry.getKey(), entry.getValue());
  662. }
  663. httpInvoker.httpMethod(HttpInvoker.HttpMethod.GET);
  664. if (callbackURL != null) {
  665. httpInvoker.parameter(
  666. "callbackURL", String.valueOf(callbackURL));
  667. }
  668. httpInvoker.path(
  669. _builder._scheme + "://" + _builder._host + ":" +
  670. _builder._port + _builder._contextPath +
  671. "/o/headless-commerce-delivery-cart/v1.0/carts/{cartId}/payment-url");
  672. httpInvoker.path("cartId", cartId);
  673. httpInvoker.userNameAndPassword(
  674. _builder._login + ":" + _builder._password);
  675. return httpInvoker.invoke();
  676. }
  677. public Page<Cart> getChannelCartsPage(
  678. Long accountId, Long channelId, Pagination pagination)
  679. throws Exception {
  680. HttpInvoker.HttpResponse httpResponse =
  681. getChannelCartsPageHttpResponse(
  682. accountId, channelId, pagination);
  683. String content = httpResponse.getContent();
  684. if ((httpResponse.getStatusCode() / 100) != 2) {
  685. _logger.log(
  686. Level.WARNING,
  687. "Unable to process HTTP response content: " + content);
  688. _logger.log(
  689. Level.WARNING,
  690. "HTTP response message: " + httpResponse.getMessage());
  691. _logger.log(
  692. Level.WARNING,
  693. "HTTP response status code: " +
  694. httpResponse.getStatusCode());
  695. throw new Problem.ProblemException(Problem.toDTO(content));
  696. }
  697. else {
  698. _logger.fine("HTTP response content: " + content);
  699. _logger.fine(
  700. "HTTP response message: " + httpResponse.getMessage());
  701. _logger.fine(
  702. "HTTP response status code: " +
  703. httpResponse.getStatusCode());
  704. }
  705. try {
  706. return Page.of(content, CartSerDes::toDTO);
  707. }
  708. catch (Exception e) {
  709. _logger.log(
  710. Level.WARNING,
  711. "Unable to process HTTP response: " + content, e);
  712. throw new Problem.ProblemException(Problem.toDTO(content));
  713. }
  714. }
  715. public HttpInvoker.HttpResponse getChannelCartsPageHttpResponse(
  716. Long accountId, Long channelId, Pagination pagination)
  717. throws Exception {
  718. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  719. if (_builder._locale != null) {
  720. httpInvoker.header(
  721. "Accept-Language", _builder._locale.toLanguageTag());
  722. }
  723. for (Map.Entry<String, String> entry :
  724. _builder._headers.entrySet()) {
  725. httpInvoker.header(entry.getKey(), entry.getValue());
  726. }
  727. for (Map.Entry<String, String> entry :
  728. _builder._parameters.entrySet()) {
  729. httpInvoker.parameter(entry.getKey(), entry.getValue());
  730. }
  731. httpInvoker.httpMethod(HttpInvoker.HttpMethod.GET);
  732. if (pagination != null) {
  733. httpInvoker.parameter(
  734. "page", String.valueOf(pagination.getPage()));
  735. httpInvoker.parameter(
  736. "pageSize", String.valueOf(pagination.getPageSize()));
  737. }
  738. httpInvoker.path(
  739. _builder._scheme + "://" + _builder._host + ":" +
  740. _builder._port + _builder._contextPath +
  741. "/o/headless-commerce-delivery-cart/v1.0/channels/{channelId}/account/{accountId}/carts");
  742. httpInvoker.path("accountId", accountId);
  743. httpInvoker.path("channelId", channelId);
  744. httpInvoker.userNameAndPassword(
  745. _builder._login + ":" + _builder._password);
  746. return httpInvoker.invoke();
  747. }
  748. public Cart postChannelCart(Long channelId, Cart cart)
  749. throws Exception {
  750. HttpInvoker.HttpResponse httpResponse = postChannelCartHttpResponse(
  751. channelId, cart);
  752. String content = httpResponse.getContent();
  753. if ((httpResponse.getStatusCode() / 100) != 2) {
  754. _logger.log(
  755. Level.WARNING,
  756. "Unable to process HTTP response content: " + content);
  757. _logger.log(
  758. Level.WARNING,
  759. "HTTP response message: " + httpResponse.getMessage());
  760. _logger.log(
  761. Level.WARNING,
  762. "HTTP response status code: " +
  763. httpResponse.getStatusCode());
  764. throw new Problem.ProblemException(Problem.toDTO(content));
  765. }
  766. else {
  767. _logger.fine("HTTP response content: " + content);
  768. _logger.fine(
  769. "HTTP response message: " + httpResponse.getMessage());
  770. _logger.fine(
  771. "HTTP response status code: " +
  772. httpResponse.getStatusCode());
  773. }
  774. try {
  775. return CartSerDes.toDTO(content);
  776. }
  777. catch (Exception e) {
  778. _logger.log(
  779. Level.WARNING,
  780. "Unable to process HTTP response: " + content, e);
  781. throw new Problem.ProblemException(Problem.toDTO(content));
  782. }
  783. }
  784. public HttpInvoker.HttpResponse postChannelCartHttpResponse(
  785. Long channelId, Cart cart)
  786. throws Exception {
  787. HttpInvoker httpInvoker = HttpInvoker.newHttpInvoker();
  788. httpInvoker.body(cart.toString(), "application/json");
  789. if (_builder._locale != null) {
  790. httpInvoker.header(
  791. "Accept-Language", _builder._locale.toLanguageTag());
  792. }
  793. for (Map.Entry<String, String> entry :
  794. _builder._headers.entrySet()) {
  795. httpInvoker.header(entry.getKey(), entry.getValue());
  796. }
  797. for (Map.Entry<String, String> entry :
  798. _builder._parameters.entrySet()) {
  799. httpInvoker.parameter(entry.getKey(), entry.getValue());
  800. }
  801. httpInvoker.httpMethod(HttpInvoker.HttpMethod.POST);
  802. httpInvoker.path(
  803. _builder._scheme + "://" + _builder._host + ":" +
  804. _builder._port + _builder._contextPath +
  805. "/o/headless-commerce-delivery-cart/v1.0/channels/{channelId}/carts");
  806. httpInvoker.path("channelId", channelId);
  807. httpInvoker.userNameAndPassword(
  808. _builder._login + ":" + _builder._password);
  809. return httpInvoker.invoke();
  810. }
  811. private CartResourceImpl(Builder builder) {
  812. _builder = builder;
  813. }
  814. private static final Logger _logger = Logger.getLogger(
  815. CartResource.class.getName());
  816. private Builder _builder;
  817. }
  818. }