-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathClient.java
More file actions
1121 lines (958 loc) · 48.1 KB
/
Client.java
File metadata and controls
1121 lines (958 loc) · 48.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.afrozaar.wordpress.wpapi.v2;
import com.afrozaar.wordpress.wpapi.v2.api.Contexts;
import com.afrozaar.wordpress.wpapi.v2.exception.*;
import com.afrozaar.wordpress.wpapi.v2.model.*;
import com.afrozaar.wordpress.wpapi.v2.request.Request;
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;
import com.afrozaar.wordpress.wpapi.v2.response.CustomRenderableParser;
import com.afrozaar.wordpress.wpapi.v2.response.PagedResponse;
import com.afrozaar.wordpress.wpapi.v2.util.AuthUtil;
import com.afrozaar.wordpress.wpapi.v2.util.MavenProperties;
import com.afrozaar.wordpress.wpapi.v2.util.Tuples.Tuple2;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.beanutils.BeanUtils;
import org.assertj.core.util.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.afrozaar.wordpress.wpapi.v2.util.FieldExtractor.extractField;
import static com.afrozaar.wordpress.wpapi.v2.util.Tuples.tuple;
import static java.lang.String.format;
import static java.net.URLDecoder.decode;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.Optional.ofNullable;
public class Client implements Wordpress {
private static final String DEFAULT_CONTEXT = "/wp-json/wp/v2";
private static final Logger LOG = LoggerFactory.getLogger(Client.class);
private static final String META_KEY = "key";
private static final String META_VALUE = "value";
private static final String FORCE = "force";
private static final String CONTEXT_ = "context";
private static final String REASSIGN = "reassign";
private static final String VIEW = "view";
private static final String DATA = "data";
private static final String VERSION = "version";
private static final String ARTIFACT_ID = "artifactId";
protected final RestTemplate restTemplate;
private final Predicate<Link> next = link -> Strings.NEXT.equals(link.getRel());
private final Predicate<Link> previous = link -> Strings.PREV.equals(link.getRel());
private final Tuple2<String, String> userAgentTuple;
public final String context;
public final String baseUrl;
public final String username;
public final String password;
public final boolean debug;
public final boolean permalinkEndpoint;
private Boolean canDeleteMetaViaPost = null;
{
Properties properties = MavenProperties.getProperties();
userAgentTuple = tuple("User-Agent", format("%s/%s", properties.getProperty(ARTIFACT_ID), properties.getProperty(VERSION)));
}
public Client(String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug) {
this(null, baseUrl, username, password, usePermalinkEndpoint, debug, null);
}
public Client(String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug, ClientHttpRequestFactory requestFactory) {
this(null, baseUrl, username, password, usePermalinkEndpoint, debug, requestFactory);
}
public Client(String context, String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug) {
this(context, baseUrl, username, password, usePermalinkEndpoint, debug, null);
}
public Client(String context, String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug,
ClientHttpRequestFactory requestFactory) {
this.context = context;
this.baseUrl = baseUrl;
this.username = username;
this.password = password;
this.debug = debug;
this.permalinkEndpoint = usePermalinkEndpoint;
final ObjectMapper emptyArrayAsNullObjectMapper = Jackson2ObjectMapperBuilder.json()
.featuresToEnable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)
.featuresToEnable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY).build();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<>());
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter(emptyArrayAsNullObjectMapper));
//messageConverters.add(new MappingJackson2HttpMessageConverter());
restTemplate = new RestTemplate(messageConverters);
if (requestFactory != null) {
restTemplate.setRequestFactory(requestFactory);
}
}
@Override
public String getContext() {
return ofNullable(this.context).orElse(DEFAULT_CONTEXT);
}
@Override
public Post createPost(final Map<String, Object> postFields, PostStatus status) throws PostCreateException {
final ImmutableMap<String, Object> post = new ImmutableMap.Builder<String, Object>().putAll(postFields).put("status", status.value).build();
try {
return doExchange1(Request.POSTS, HttpMethod.POST, Post.class, forExpand(), null, post, MediaType.APPLICATION_JSON).getBody();
} catch (HttpClientErrorException e) {
throw new PostCreateException(e);
}
}
@Override
public Post createPost(Post post, PostStatus status) throws PostCreateException {
return createPost(fieldsFrom(post), status);
}
@Override
public Post getCustomPost(Long id, String requestPath) throws PostNotFoundException {
return getPost(id, requestPath, Contexts.VIEW);
}
@Override
public Post getPost(Long id) throws PostNotFoundException {
return getPost(id, Contexts.VIEW);
}
@Override
public Post getPost(Long id, String context) throws PostNotFoundException {
return getPost(id, Request.POST, context);
}
public Post getPost(Long id, String postTypeName, String context) throws PostNotFoundException {
try {
return doExchange1(postTypeName, HttpMethod.GET, Post.class, forExpand(id), ImmutableMap.of(CONTEXT_, context), null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new PostNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public Post updatePost(Post post) {
final ResponseEntity<Post> exchange =
doExchange1(Request.POST, HttpMethod.PUT, Post.class, forExpand(post.getId()), ImmutableMap.of(), fieldsFrom(post));
return exchange.getBody();
}
@Override
public Post updatePostField(Long postId, String field, Object value) {
return doExchange1(Request.POST, HttpMethod.PUT, Post.class, forExpand(postId), null, ImmutableMap.of(field, value)).getBody();
}
@Override
public Post deletePost(Post post) {
final ResponseEntity<Post> exchange = doExchange1(Request.POST, HttpMethod.DELETE, Post.class, forExpand(post.getId()), null,
null);// Deletion of a post returns the post's data before removing it.
Preconditions.checkArgument(exchange.getStatusCode().is2xxSuccessful());
return exchange.getBody();
}
@Override
public Comment updateComment(Comment comment) {
final ResponseEntity<Comment> exchange =
doExchange1(Request.COMMENT, HttpMethod.POST, Comment.class, forExpand(comment.getId()), ImmutableMap.of(), fieldsFrom(comment));
return exchange.getBody();
}
@Override
public <T> PagedResponse<T> search(SearchRequest<T> search) {
final URI uri = search.usingClient(this).build().toUri();
return getPagedResponse(uri, search.getClazz());
}
@SuppressWarnings("unchecked")
@Override
public Media createMedia(Media media, Resource resource) throws WpApiParsedException {
Objects.requireNonNull(resource.getFilename(),
"The resource used to create a media item does not provide a filename. Please supply a Resource that overrides getFilename()");
try {
final MultiValueMap<String, Object> uploadMap = new LinkedMultiValueMap<>();
BiConsumer<String, Object> p = (index, value) -> ofNullable(value).ifPresent(v -> uploadMap.add(index, v));
p.accept("title", extractField(Media::getTitle, media).orElse(null));
p.accept("post", media.getPost());
p.accept("alt_text", media.getAltText());
p.accept("caption", media.getCaption());
p.accept("description", media.getDescription());
uploadMap.add("file", resource);
return CustomRenderableParser.parseMedia(doExchange1(Request.MEDIAS, HttpMethod.POST, String.class, forExpand(), null, uploadMap).getBody());
} catch (HttpClientErrorException | HttpServerErrorException e) {
throw WpApiParsedException.of(e);
}
}
@Override
public Post setPostFeaturedMedia(Long postId, Media media) {
Preconditions.checkArgument("image".equals(media.getMediaType()), "Can not set non-image media type as a featured image on a post.");
return updatePostField(postId, "featured_media", media.getId());
}
@Override
public List<Media> getPostMedias(Long postId) {
return getPostMedias(postId, Contexts.EDIT);
}
@Override
public List<Media> getPostMedias(Long postId, @Nullable String context) {
List<Media> collected = new ArrayList<>();
PagedResponse<Media> pagedResponse = this.getPagedResponse(Request.POST_MEDIAS, Media.class, String.valueOf(postId), context);
collected.addAll(pagedResponse.getList());
while (pagedResponse.hasNext()) {
pagedResponse = this.traverse(pagedResponse, PagedResponse.NEXT);
collected.addAll(pagedResponse.getList());
}
return collected;
}
@Override
public List<Media> getMedia() {
List<Media> collected = new ArrayList<>();
PagedResponse<Media> pagedResponse = this.getPagedResponse(Request.MEDIAS, Media.class);
collected.addAll(pagedResponse.getList());
while (pagedResponse.hasNext()) {
pagedResponse = this.traverse(pagedResponse, PagedResponse.NEXT);
collected.addAll(pagedResponse.getList());
}
return collected;
}
@Override
public Media getMedia(Long id) {
return getMedia(id, null);
}
@Override
public Media getMedia(Long id, @Nullable String context) {
final ImmutableMap<String, Object> queryParams = ImmutableMap.of(CONTEXT_, ofNullable(context).orElse(Contexts.EDIT));
return CustomRenderableParser.parse(doExchange1(Request.MEDIA, HttpMethod.GET, String.class, forExpand(id), queryParams, null), Media.class);
}
@Override
public Media updateMedia(Media media) {
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
BiConsumer<String, Object> p = (key, value) -> ofNullable(value).ifPresent(v -> builder.put(key, v));
p.accept("title", extractField(Media::getTitle, media).orElse(null));
p.accept("post", media.getPost());
p.accept("alt_text", media.getAltText());
p.accept("caption", media.getCaption());
p.accept("description", media.getDescription());
return CustomRenderableParser
.parse(doExchange1(Request.MEDIA, HttpMethod.POST, String.class, forExpand(media.getId()), null, builder.build()), Media.class);
}
@Override
public boolean deleteMedia(Media media, boolean force) {
final ResponseEntity<String> exchange =
doExchange1(Request.MEDIA, HttpMethod.DELETE, String.class, forExpand(media.getId()), ImmutableMap.of(FORCE, force), null);
return exchange.getStatusCode().is2xxSuccessful();
}
@Override
public boolean deleteMedia(Media media) {
// We don't care to deserialize the received response back into a media object.
final ResponseEntity<String> exchange = doExchange1(Request.MEDIA, HttpMethod.DELETE, String.class, forExpand(media.getId()), null, null);
return exchange.getStatusCode().is2xxSuccessful();
}
@Override
public PostMeta createMeta(Long postId, String key, String value) {
final Map<String, String> body = ImmutableMap.of(META_KEY, key, META_VALUE, value);
final ResponseEntity<PostMeta> exchange = doExchange1(Request.METAS, HttpMethod.POST, PostMeta.class, forExpand(postId), null, body,
MediaType.APPLICATION_JSON);
return exchange.getBody();
}
@Override
public PostMeta createCustomPostMeta(Long postId, String key, String value, String customPostTypeName) {
final Map<String, String> body = ImmutableMap.of(META_KEY, key, META_VALUE, value);
final ResponseEntity<PostMeta> exchange =
doExchange1(Request.CUSTOM_POST_METAS, HttpMethod.POST, PostMeta.class, forExpand(customPostTypeName, postId), null, body,
MediaType.APPLICATION_JSON);
return exchange.getBody();
}
@Override
public List<PostMeta> getPostMetas(Long postId) {
final ResponseEntity<PostMeta[]> exchange = doExchange1(Request.METAS, HttpMethod.GET, PostMeta[].class, forExpand(postId), null, null);
return Arrays.asList(exchange.getBody());
}
@Override
public PostMeta getPostMeta(Long postId, Long metaId) {
final ResponseEntity<PostMeta> exchange = doExchange1(Request.META, HttpMethod.GET, PostMeta.class, forExpand(postId, metaId), null, null);
return exchange.getBody();
}
@Override
public List<PostMeta> getCustomPostMetas(Long postId, String customPostTypeName) {
final ResponseEntity<PostMeta[]> exchange =
doExchange1(Request.CUSTOM_POST_METAS, HttpMethod.GET, PostMeta[].class, forExpand(customPostTypeName, postId), null, null);
return Arrays.asList(exchange.getBody());
}
@Override
public PostMeta getCustomPostMeta(Long postId, Long metaId, String customPostTypeName) {
final ResponseEntity<PostMeta> exchange =
doExchange1(Request.CUSTOM_POST_META, HttpMethod.GET, PostMeta.class, forExpand(customPostTypeName, postId, metaId), null, null);
return exchange.getBody();
}
@Override
public PostMeta updatePostMetaValue(Long postId, Long metaId, String value) {
return updatePostMeta(postId, metaId, null, value);
}
@Override
public PostMeta updatePostMeta(Long postId, Long metaId, String key, String value) {
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
BiConsumer<String, Object> biConsumer = (key1, value1) -> ofNullable(value1).ifPresent(v -> builder.put(key1, v));
biConsumer.accept(META_KEY, key);
biConsumer.accept(META_VALUE, value);
final ResponseEntity<PostMeta> exchange = doExchange1(Request.META, HttpMethod.POST, PostMeta.class, forExpand(postId, metaId), null, builder.build());
return exchange.getBody();
}
@Override
public PostMeta updateCustomPostMeta(Long postId, Long metaId, String key, String value, String customPostTypeName) {
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
BiConsumer<String, Object> biConsumer = (key1, value1) -> ofNullable(value1).ifPresent(v -> builder.put(key1, v));
biConsumer.accept(META_KEY, key);
biConsumer.accept(META_VALUE, value);
final ResponseEntity<PostMeta> exchange =
doExchange1(Request.CUSTOM_POST_META, HttpMethod.POST, PostMeta.class, forExpand(customPostTypeName, postId, metaId), null, builder.build());
return exchange.getBody();
}
private BiFunction<Long, Long, Boolean> supportsMetaDeleteViaPostMethod = (pid, mid) -> {
if (nonNull(canDeleteMetaViaPost)) {
return canDeleteMetaViaPost;
}
try {
Function<Map, Boolean> expected = map -> nonNull(map) && Stream.of("endpoints", "methods", "namespace").allMatch(map::containsKey) && Objects
.equals(((ArrayList) map.get("methods")).get(0), "POST");
final ResponseEntity<Map> responseEntity = doExchange1(Request.META_POST_DELETE, HttpMethod.OPTIONS, Map.class, forExpand(pid, mid), null, null);
final Map body = responseEntity.getBody();
canDeleteMetaViaPost = responseEntity.getStatusCode().is2xxSuccessful() && expected.apply(body);
// need a getter for getBaseUrl as it is used in this function (and eclipse compiler doesn't like it being used directly cause it says it's not initalised
LOG.info("Wordpress instance at {} supports deleting meta via POST /posts/:pid/meta/:mid/delete : {}", getBaseUrl(), canDeleteMetaViaPost);
return canDeleteMetaViaPost;
} catch (Exception jme) {
canDeleteMetaViaPost = false;
//com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
if (!(jme instanceof JsonMappingException)) {
LOG.error("Unexpected exception pinging for POST /posts/:pid/meta/:mid/delete", jme);
}
return canDeleteMetaViaPost;
}
};
@Override
public boolean deletePostMeta(Long postId, Long metaId) {
return deletePostMeta(postId, metaId, null);
}
@Override
public boolean deletePostMeta(Long postId, Long metaId, Boolean force) {
if (supportsMetaDeleteViaPostMethod.apply(postId, metaId)) {
// deleting meta via meta POST is available, so use that to delete.
final ResponseEntity<Map> result = doExchange1(Request.META_POST_DELETE, HttpMethod.POST, Map.class, forExpand(postId, metaId),
isNull(force) ? null : ImmutableMap.of(FORCE, force), null);
return result.getStatusCode().is2xxSuccessful() && "Deleted meta".equals(result.getBody().get("message"));
} else {
// attempt normal delete
final ResponseEntity<Map> exchange =
doExchange1(Request.META, HttpMethod.DELETE, Map.class, forExpand(postId, metaId), isNull(force) ? null : ImmutableMap.of(FORCE, force),
null);
Preconditions.checkArgument(exchange.getStatusCode().is2xxSuccessful(),
format("Expected success on post meta delete request: /posts/%s/meta/%s", postId, metaId));
return exchange.getStatusCode().is2xxSuccessful();
}
}
@Override
public boolean deleteCustomPostMeta(Long postId, Long metaId, Boolean force, String customPostTypeName) {
if (supportsMetaDeleteViaPostMethod.apply(postId, metaId)) {
// deleting meta via meta POST is available, so use that to delete.
final ResponseEntity<Map> result =
doExchange1(Request.CUSTOM_META_POST_DELETE, HttpMethod.POST, Map.class, forExpand(customPostTypeName, postId, metaId),
isNull(force) ? null : ImmutableMap.of(FORCE, force), null);
return result.getStatusCode().is2xxSuccessful() && "Deleted meta".equals(result.getBody().get("message"));
} else {
// attempt normal delete
final ResponseEntity<Map> exchange =
doExchange1(Request.CUSTOM_POST_META, HttpMethod.DELETE, Map.class, forExpand(customPostTypeName, postId, metaId),
isNull(force) ? null : ImmutableMap.of(FORCE, force), null);
Preconditions.checkArgument(exchange.getStatusCode().is2xxSuccessful(),
format("Expected success on post meta delete request: /posts/%s/meta/%s", postId, metaId));
return exchange.getStatusCode().is2xxSuccessful();
}
}
@SuppressWarnings("unchecked")
@Override
public List<Taxonomy> getTaxonomies() {
final ResponseEntity<Map> exchange = doExchange1(Request.TAXONOMIES, HttpMethod.GET, Map.class, forExpand(), null, null);
final Map body = exchange.getBody();
List<Taxonomy> toReturn = new ArrayList<>();
body.forEach((key, obj) -> {
try {
Taxonomy target = new Taxonomy();
Map source = (Map) obj;
BeanUtils.populate(target, source);
toReturn.add(target);
} catch (IllegalAccessException | InvocationTargetException e) {
LOG.error("Error ", e);
}
});
return toReturn;
}
@Override
public Taxonomy getTaxonomy(String slug) {
return doExchange1(Request.TAXONOMY, HttpMethod.GET, Taxonomy.class, forExpand(slug), null, null).getBody();
}
@Override
public List<Term> getTerms(String taxonomy) {
List<Term> collected = new ArrayList<>();
PagedResponse<Term> pagedResponse = this.getPagedResponse(Request.TERMS, Term.class, taxonomy);
collected.addAll(pagedResponse.getList());
while (pagedResponse.hasNext()) {
pagedResponse = this.traverse(pagedResponse, PagedResponse.NEXT);
collected.addAll(pagedResponse.getList());
}
return collected;
}
@Override
public Term getTerm(String taxonomy, Long id) throws TermNotFoundException {
try {
return doExchange1(Request.TERM, HttpMethod.GET, Term.class, forExpand(taxonomy, id), null, null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new TermNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public Term updateTerm(String taxonomy, Term term) {
return doExchange1(Request.TERM, HttpMethod.POST, Term.class, forExpand(taxonomy, term.getId()), null, term.asMap()).getBody();
}
@Override
public Term deleteTerm(String taxonomy, Term term) throws TermNotFoundException {
try {
return doExchange1(Request.TERM, HttpMethod.DELETE, Term.class, forExpand(taxonomy, term.getId()), null, null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new TermNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public List<Term> deleteTerms(String taxonomy, Term... terms) {
List<Term> deletedTerms = new ArrayList<>(terms.length);
for (Term term : terms) {
try {
deletedTerms.add(deleteTerm(taxonomy, term));
} catch (TermNotFoundException e) {
LOG.error("Error ", e);
}
}
return deletedTerms;
}
@Override
public Term createTag(Term tagTerm) throws WpApiParsedException {
try {
return doExchange1(Request.TAGS, HttpMethod.POST, Term.class, forExpand(), tagTerm.asMap(), null).getBody();
} catch (HttpClientErrorException | HttpServerErrorException e) {
final WpApiParsedException exception = WpApiParsedException.of(e);
LOG.error("Could not create tag '{}'. {} ", tagTerm.getName(), exception.getMessage(), exception);
throw exception;
}
}
@Override
public List<Term> getTags() {
return getAllTermsForEndpoint(Request.TAGS);
}
@Override
public Term getTag(Long id) throws TermNotFoundException {
try {
return doExchange1(Request.TAG, HttpMethod.GET, Term.class, forExpand(id), null, null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new TermNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public Term deleteTag(Term tagTerm) throws TermNotFoundException {
return deleteTag(tagTerm, false);
}
@Override
public Term deleteTag(Term tagTerm, boolean force) throws TermNotFoundException {
try {
Map<String, Object> queryParams = force ? ImmutableMap.of("force", true) : null;
final ResponseEntity<String> tResponseEntity =
doExchange1(Request.TAG, HttpMethod.DELETE, String.class, forExpand(tagTerm.getId()), queryParams, null);
final DeleteResponse<Term> termDeleteResponse = CustomRenderableParser.parseDeleteResponse(tResponseEntity, Term.class);
final Term previous = termDeleteResponse.getPrevious();
LOG.debug("Deleted term @{}/'{}' of taxonomy '{}': {}", previous.getId(), previous.getName(), previous.getTaxonomySlug(),
termDeleteResponse.getDeleted());
return previous;
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new TermNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public Term createPostTag(Post post, Term tag) throws WpApiParsedException {
final Term termToUse = nonNull(tag.getId()) ? tag : createTag(tag);
final List<Term> postTags = new ArrayList<>(getPostTags(post));
postTags.add(termToUse);
final List<Long> tagIds = postTags.stream().map(Term::getId).collect(Collectors.toList());
//Map<String, Object> body = ImmutableMap.of("tags", tagIds);
updatePostField(post.getId(), "tags", tagIds);
return termToUse;
//return doExchange1(Request.POST, HttpMethod.POST, Term.class, forExpand(post.getId(), null, termToUse.getId()), null, body).getBody();
}
@Override
public List<Term> getPostTags(Post post) {
return getAllTermsForEndpoint(Request.POST_TAGS, post.getId().toString());
}
@Override
public Term deletePostTag(Post post, Term tagTerm, boolean force) throws TermNotFoundException {
try {
final List<Term> postTags = new ArrayList<>(getPostTags(post));
final Optional<Term> found = postTags.stream().filter(term -> Objects.equals(term.getId(), tagTerm.getId())).findFirst();
if (found.isPresent()) {
postTags.remove(found.get());
updatePostField(post.getId(), "tags", termIds.apply(postTags));
return tagTerm;
} else {
throw new RuntimeException("Expected to find term in post's term list.");
}
//return doExchange1(Request.POST_TERM, HttpMethod.DELETE, Term.class, forExpand(post.getId(), Taxonomies.TAGS, tagTerm.getId()), ImmutableMap.of(FORCE, force), null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new TermNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public Term getPostTag(Post post, Term tagTerm) throws TermNotFoundException {
try {
return doExchange1(Request.POST_TERM, HttpMethod.GET, Term.class, forExpand(post.getId(), TAGS, tagTerm.getId()), null, null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new TermNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public Term updateTag(Term tag) {
return doExchange1(Request.TAG, HttpMethod.POST, Term.class, forExpand(tag.getId()), null, tag.asMap()).getBody();
}
@Override
public Term getCategory(Long id) {
return doExchange1(Request.CATEGORY, HttpMethod.GET, Term.class, forExpand(id), null, null).getBody();
}
@Override
public List<Term> getCategories() {
return getAllTermsForEndpoint(Request.CATEGORIES);
}
@Override
public List<Term> getAllTermsForEndpoint(final String endpoint, String... expandParams) {
List<Term> collected = new ArrayList<>();
PagedResponse<Term> pagedResponse = this.getPagedResponse(endpoint, Term.class, expandParams);
collected.addAll(pagedResponse.getList());
while (pagedResponse.hasNext()) {
pagedResponse = this.traverse(pagedResponse, PagedResponse.NEXT);
collected.addAll(pagedResponse.getList());
}
return collected;
}
@Override
public Term createCategory(Term categoryTerm) {
return doExchange1(Request.CATEGORIES, HttpMethod.POST, Term.class, forExpand(), null, categoryTerm.asMap(), MediaType.APPLICATION_JSON).getBody();
}
@Override
public Term deleteCategory(Term categoryTerm) throws TermNotFoundException {
return deleteCategory(categoryTerm, false);
}
@Override
public Term deleteCategory(Term categoryTerm, boolean force) throws TermNotFoundException {
try {
Map<String, Object> queryParams = force ? ImmutableMap.of("force", true) : null;
return doExchange1(Request.CATEGORY, HttpMethod.DELETE, Term.class, forExpand(categoryTerm.getId()), queryParams, null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new TermNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public List<Term> deleteCategories(Term... terms) {
return deleteCategories(false, terms);
}
@Override
public List<Term> deleteCategories(boolean force, Term... terms) {
List<Term> deletedTerms = new ArrayList<>(terms.length);
for (Term term : terms) {
try {
deletedTerms.add(deleteCategory(term, force));
} catch (TermNotFoundException e) {
LOG.error("Error ", e);
}
}
return deletedTerms;
}
@Override
public Term updateCategory(Term categoryTerm) {
return doExchange1(Request.CATEGORY, HttpMethod.POST, Term.class, forExpand(categoryTerm.getId()), null, categoryTerm.asMap()).getBody();
}
@Override
public Page createPage(Page page, PostStatus status) {
final Map<String, Object> map = page.asMap();
final ImmutableMap<String, Object> pageFields = new ImmutableMap.Builder<String, Object>().putAll(map).put("status", status.value).build();
return doExchange1(Request.PAGES, HttpMethod.POST, Page.class, forExpand(), null, pageFields).getBody();
}
@Override
public Page getPage(Long pageId) throws PageNotFoundException {
try {
return getPage(pageId, VIEW);
} catch (HttpClientErrorException e) {
throw new PageNotFoundException(e);
}
}
@Override
public Page getPage(Long pageId, String context) {
return doExchange1(Request.PAGE, HttpMethod.GET, Page.class, forExpand(pageId), ImmutableMap.of(CONTEXT_, context), null).getBody();
}
@Override
public Page updatePage(Page page) {
return doExchange1(Request.PAGE, HttpMethod.POST, Page.class, forExpand(page.getId()), null, page.asMap()).getBody();
}
@Override
public Page deletePage(Page page) {
return doExchange1(Request.PAGE, HttpMethod.DELETE, Page.class, forExpand(page.getId()), null, null).getBody();
}
@Override
public Page deletePage(Page page, boolean force) {
return doExchange1(Request.PAGE, HttpMethod.DELETE, Page.class, forExpand(page.getId()), ImmutableMap.of(FORCE, force), null).getBody();
}
@Override
public List<User> getUsers() {
return getUsers(Contexts.VIEW);
}
@Override
public List<User> getUsers(final String contextType) {
List<User> collected = new ArrayList<>();
PagedResponse<User> usersResponse = this.getPagedResponse(Request.USERS_WITH_CONTEXT, User.class, contextType);
collected.addAll(usersResponse.getList());
while (usersResponse.hasNext()) {
usersResponse = traverse(usersResponse, PagedResponse.NEXT);
collected.addAll(usersResponse.getList());
}
return collected;
}
@SuppressWarnings("unchecked")
@Override
public User createUser(User user, String username, String password) throws WpApiParsedException {
final MultiValueMap userAsMap = userMap.apply(user);
userAsMap.add("username", username); // Required: true
userAsMap.add("password", password); // Required: true
try {
return doExchange1(Request.USERS, HttpMethod.POST, User.class, forExpand(), null, userAsMap).getBody();
} catch (HttpServerErrorException | HttpClientErrorException e) {
try {
ParsedRestException restException = ParsedRestException.of(e);
switch (restException.getCode()) {
case ExceptionCodes.INVALID_PARAM:
throw new InvalidParameterException(restException);
case ExceptionCodes.EXISTING_USER_LOGIN:
throw new UsernameAlreadyExistsException(restException);
case ExceptionCodes.EXISTING_USER_EMAIL:
throw new UserEmailAlreadyExistsException(restException);
}
} catch (RuntimeException rte) {
LOG.info("error parsing {}", e.getResponseBodyAsString(), e);
}
throw e;
}
}
@Override
public User getUser(long userId) throws UserNotFoundException {
return getUser(userId, null);
}
@Override
public User getUser(long userId, String context) throws UserNotFoundException {
final Map<String, Object> params = context == null ? null : ImmutableMap.of(CONTEXT_, context);
try {
return doExchange1(Request.USER, HttpMethod.GET, User.class, forExpand(userId), params, null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new UserNotFoundException(e);
} else {
throw e;
}
}
}
@Override
public User deleteUser(User user) {
return deleteUser(user, null);
}
@Override
public User deleteUser(User user, Long reassign) {
try {
return doExchange1(Request.USER, HttpMethod.DELETE, User.class, forExpand(user.getId()),
ImmutableMap.of(FORCE, true, REASSIGN, (nonNull(reassign) ? reassign : 1L)), null).getBody();
} catch (HttpClientErrorException | HttpServerErrorException e) {
final WpApiParsedException of = WpApiParsedException.of(e);
LOG.error("Error Deleting user {}", user.getId(), of);
throw new RuntimeException(of);
}
}
@Override
public Object getCustom(String customPath, String context, Class clazz) throws NotFoundException {
final Map<String, Object> params = context == null ? null : ImmutableMap.of(CONTEXT_, context);
try {
return doExchange1(customPath, HttpMethod.GET, clazz, new Object[0], params, null).getBody();
} catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
throw new NotFoundException(e);
} else {
throw e;
}
}
}
@Override
public void putCustom(String customPath) {
doExchange1(customPath, HttpMethod.PUT, null, new Object[0], null, null).getBody();
}
@Override
public User updateUser(User user) {
return doExchange1(Request.USER, HttpMethod.POST, User.class, forExpand(user.getId()), null, userMap.apply(user)).getBody();
}
@SuppressWarnings("unchecked")
@Override
public <T> PagedResponse<T> getPagedResponse(String context, Class<T> typeRef, String... expandParams) {
final URI uri = Request.of(context).usingClient(this).buildAndExpand((Object[]) expandParams).toUri();
return getPagedResponse(uri, typeRef);
}
@SuppressWarnings("unchecked")
@Override
public <T> PagedResponse<T> getPagedResponse(final URI uri, Class<T> typeRef) {
try {
final ResponseEntity<String> exchange = doExchange0(HttpMethod.GET, uri, String.class, null, null);
final String body1 = exchange.getBody();
//LOG.debug("about to parse response for paged response {}: {}", typeRef, body1);
final T[] parse = CustomRenderableParser.parse(body1, (Class<T[]>) Class.forName("[L" + typeRef.getName() + ";"));
final HttpHeaders headers = exchange.getHeaders();
final List<Link> links = parseLinks(headers);
final List<T> body = Arrays.asList(parse); // Ugly... but the only way to get the generic stuff working
return PagedResponse.Builder.aPagedResponse(typeRef)
.withPages(headers)
.withPosts(body)
.withSelf(uri.toASCIIString())
.withNext(link(links, next))
.withPrevious(link(links, previous))
.build();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
@Override
public <T> PagedResponse<T> traverse(PagedResponse<T> response, Function<PagedResponse<?>, String> direction) {
final URI uri = response.getUri(direction);
return getPagedResponse(uri, response.getClazz());
}
public List<Link> parseLinks(HttpHeaders headers) {
//Link -> [<http://johan-wp/wp-json/wp/v2/posts?page=2>; rel="next"]
Optional<List<String>> linkHeader = ofNullable(headers.get(Strings.HEADER_LINK));
return linkHeader
.map(List::stream)
.map(Stream::findFirst)
.map(rawResponse -> {
final String[] links = rawResponse.get().split(", ");
return Arrays.stream(links).map(link -> { // <http://johan-wp/wp-json/wp/v2/posts?page=2>; rel="next"
String[] linkData = link.split("; ");
final String href = linkData[0].replace("<", "").replace(">", "");
final String rel = linkData[1].substring(4).replace("\"", "");
return Link.of(fixQuery(href), rel);
}).collect(Collectors.toList());
})
.orElse(Collections.emptyList());
}
private String fixQuery(String href) {
final UriComponents build = UriComponentsBuilder.fromHttpUrl(href).build();
final MultiValueMap<String, String> queryParams = build.getQueryParams();
final MultiValueMap<String, String> queryParamsFixed = new LinkedMultiValueMap<>();
queryParams.forEach((key, values) -> queryParamsFixed.put(decode(key), values.stream().map(URLDecoder::decode).collect(Collectors.toList())));
return UriComponentsBuilder.fromPath(build.getPath())
.scheme(build.getScheme())
.queryParams(queryParamsFixed)
.fragment(build.getFragment())
.port(build.getPort())
.host(build.getHost()).build().toUriString();
}
@VisibleForTesting
@SuppressWarnings("unchecked")
protected Map<String, Object> fieldsFrom(Post post) {
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
BiConsumer<String, Object> biConsumer = (key, value) -> ofNullable(value).ifPresent(v -> builder.put(key, v));
List<String> processableFields = Arrays.asList(
"author",
"categories",
"comment_status",
"content",
"date",
"featured_media",
"format",
"excerpt",
"modified_gmt",
"ping_status",
//"slug",
//"status",
"sticky",
"tags",
"title",
"type"
);
// types ignored for now: slug, status, type
Arrays.stream(post.getClass().getDeclaredFields())
.filter(field -> field.getAnnotationsByType(JsonProperty.class).length > 0)
.map(field -> tuple(field, field.getAnnotationsByType(JsonProperty.class)[0]))
.filter(fieldTuple -> processableFields.contains(fieldTuple.v2.value()))
.forEach(field -> {
try {
ReflectionUtils.makeAccessible(field.v1);
Object theField = field.v1.get(post);
if (nonNull(theField)) {
final Object value;
if (theField instanceof RenderableField) {
value = ((RenderableField) theField).getRendered();
} else {
value = theField;
}
biConsumer.accept(field.v2.value(), value);
}
} catch (IllegalAccessException e) {
LOG.error("Error populating post fields builder for field '{}'", field.v1.getName(), e);
}
});
return builder.build();
}