This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
/
PageServiceImpl.java
449 lines (384 loc) · 15.3 KB
/
PageServiceImpl.java
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
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.management.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.gravitee.common.http.MediaType;
import io.gravitee.common.utils.UUID;
import io.gravitee.fetcher.api.Fetcher;
import io.gravitee.fetcher.api.FetcherConfiguration;
import io.gravitee.fetcher.api.FetcherException;
import io.gravitee.management.fetcher.FetcherConfigurationFactory;
import io.gravitee.management.model.*;
import io.gravitee.management.service.ApiService;
import io.gravitee.management.service.PageService;
import io.gravitee.management.service.exceptions.PageAlreadyExistsException;
import io.gravitee.management.service.exceptions.PageNotFoundException;
import io.gravitee.management.service.exceptions.TechnicalManagementException;
import io.gravitee.plugin.fetcher.FetcherPlugin;
import io.gravitee.plugin.fetcher.FetcherPluginManager;
import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.PageRepository;
import io.gravitee.repository.management.model.Page;
import io.gravitee.repository.management.model.PageConfiguration;
import io.gravitee.repository.management.model.PageSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
/**
* @author Titouan COMPIEGNE
* @author Nicolas GERAUD (nicolas.geraud [at] graviteesource [dot] com)
* @author GraviteeSource Team
*/
@Component
public class PageServiceImpl extends TransactionalService implements PageService {
private final Logger LOGGER = LoggerFactory.getLogger(PageServiceImpl.class);
@Autowired
private PageRepository pageRepository;
@Autowired
private ApiService apiService;
@Autowired
private FetcherPluginManager fetcherPluginManager;
@Autowired
private FetcherConfigurationFactory fetcherConfigurationFactory;
@Autowired
private Configuration freemarkerConfiguration;
@Override
public List<PageListItem> findByApi(String apiId) {
try {
final Collection<Page> pages = pageRepository.findByApi(apiId);
if (pages == null) {
return emptyList();
}
return pages.stream()
.map(this::reduce)
.sorted((o1, o2) -> Integer.compare(o1.getOrder(), o2.getOrder()))
.collect(Collectors.toList());
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to get API pages using api ID {}", apiId, ex);
throw new TechnicalManagementException(
"An error occurs while trying to get API pages using api ID " + apiId, ex);
}
}
@Override
public PageEntity findById(String pageId) {
return findById(pageId, false);
}
@Override
public PageEntity findById(String pageId, boolean transform) {
try {
LOGGER.debug("Find page by ID: {}", pageId);
Optional<Page> page = pageRepository.findById(pageId);
if (page.isPresent()) {
PageEntity pageEntity = convert(page.get());
if (transform) {
transformWithTemplate(pageEntity, page.get().getApi());
}
return pageEntity;
}
throw new PageNotFoundException(pageId);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to find a page using its ID {}", pageId, ex);
throw new TechnicalManagementException(
"An error occurs while trying to find a page using its ID " + pageId, ex);
}
}
private void transformWithTemplate(PageEntity pageEntity, String api) {
try {
Template template = new Template(pageEntity.getId(), pageEntity.getContent(), freemarkerConfiguration);
ApiEntity apiEntity = apiService.findById(api);
Map<String, Object> model = new HashMap<>();
model.put("api", apiEntity);
final String content =
FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
pageEntity.setContent(content);
} catch (IOException | TemplateException ex) {
LOGGER.error("An error occurs while transforming page content for {}", pageEntity.getId(), ex);
}
}
@Override
public PageEntity create(String apiId, NewPageEntity newPageEntity) {
try {
LOGGER.debug("Create page {} for API {}", newPageEntity, apiId);
String id = UUID.toString(UUID.random());
Optional<Page> checkPage = pageRepository.findById(id);
if (checkPage.isPresent()) {
throw new PageAlreadyExistsException(id);
}
Page page = convert(newPageEntity);
if (page.getSource() != null) {
String fetchedContent = this.getContentFromFetcher(page.getSource());
if (fetchedContent != null && !fetchedContent.isEmpty()) {
page.setContent(fetchedContent);
}
}
page.setId(id);
page.setApi(apiId);
// Set date fields
page.setCreatedAt(new Date());
page.setUpdatedAt(page.getCreatedAt());
Page createdPage = pageRepository.create(page);
return convert(createdPage);
} catch (TechnicalException | FetcherException ex) {
LOGGER.error("An error occurs while trying to create {}", newPageEntity, ex);
throw new TechnicalManagementException("An error occurs while trying create " + newPageEntity, ex);
}
}
@Override
public PageEntity update(String pageId, UpdatePageEntity updatePageEntity) {
try {
LOGGER.debug("Update Page {}", pageId);
Optional<Page> optPageToUpdate = pageRepository.findById(pageId);
if (!optPageToUpdate.isPresent()) {
throw new PageNotFoundException(pageId);
}
Page pageToUpdate = optPageToUpdate.get();
Page page = convert(updatePageEntity);
if (page.getSource() != null) {
try {
String fetchedContent = this.getContentFromFetcher(page.getSource());
if (fetchedContent != null && !fetchedContent.isEmpty()) {
page.setContent(fetchedContent);
}
} catch (FetcherException e) {
throw onUpdateFail(pageId, e);
}
}
page.setId(pageId);
page.setUpdatedAt(new Date());
// Copy fields from existing values
page.setCreatedAt(pageToUpdate.getCreatedAt());
page.setType(pageToUpdate.getType());
page.setApi(pageToUpdate.getApi());
// if order change, reorder all pages
if (page.getOrder() != pageToUpdate.getOrder()) {
reorderAndSavePages(page);
return null;
} else {
Page updatedPage = pageRepository.update(page);
return convert(updatedPage);
}
} catch (TechnicalException ex) {
throw onUpdateFail(pageId, ex);
}
}
private String getContentFromFetcher(PageSource ps) throws FetcherException {
if (ps.getConfiguration().isEmpty()) {
return null;
}
try {
FetcherPlugin fetcherPlugin = fetcherPluginManager.get(ps.getType());
ClassLoader fetcherCL = fetcherPlugin.fetcher().getClassLoader();
Class<? extends FetcherConfiguration> fetcherConfigurationClass = (Class<? extends FetcherConfiguration>) fetcherCL.loadClass(fetcherPlugin.configuration().getName());
Class<? extends Fetcher> fetcherClass = (Class<? extends Fetcher>) fetcherCL.loadClass(fetcherPlugin.clazz());
FetcherConfiguration fetcherConfigurationInstance = fetcherConfigurationFactory.create(fetcherConfigurationClass, ps.getConfiguration());
Fetcher fetcher = fetcherClass.getConstructor(fetcherConfigurationClass).newInstance(fetcherConfigurationInstance);
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(fetcher.fetch()))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
}
return sb.toString();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new FetcherException(e.getMessage(), e);
}
}
private void reorderAndSavePages(final Page pageToReorder) throws TechnicalException {
final Collection<Page> pages = pageRepository.findByApi(pageToReorder.getApi());
final List<Boolean> increment = asList(true);
pages.stream()
.sorted((o1, o2) -> Integer.compare(o1.getOrder(), o2.getOrder()))
.forEachOrdered(page -> {
try {
if (page.equals(pageToReorder)) {
increment.set(0, false);
page.setOrder(pageToReorder.getOrder());
} else {
final int newOrder;
final Boolean isIncrement = increment.get(0);
if (page.getOrder() < pageToReorder.getOrder()) {
newOrder = page.getOrder() - (isIncrement ? 0 : 1);
} else if (page.getOrder() > pageToReorder.getOrder()) {
newOrder = page.getOrder() + (isIncrement? 1 : 0);
} else {
newOrder = page.getOrder() + (isIncrement? 1 : -1);
}
page.setOrder(newOrder);
}
pageRepository.update(page);
} catch (final TechnicalException ex) {
throw onUpdateFail(page.getId(), ex);
}
});
}
private TechnicalManagementException onUpdateFail(String pageId, TechnicalException ex) {
LOGGER.error("An error occurs while trying to update page {}", pageId, ex);
return new TechnicalManagementException("An error occurs while trying to update page " + pageId, ex);
}
private TechnicalManagementException onUpdateFail(String pageId, FetcherException ex) {
LOGGER.error("An error occurs while trying to update page {}", pageId, ex);
return new TechnicalManagementException("An error occurs while trying to update page " + pageId, ex);
}
@Override
public void delete(String pageName) {
try {
LOGGER.debug("Delete PAGE : {}", pageName);
pageRepository.delete(pageName);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete PAGE {}", pageName, ex);
throw new TechnicalManagementException("An error occurs while trying to delete PAGE " + pageName, ex);
}
}
@Override
public int findMaxPageOrderByApi(String apiName) {
try {
LOGGER.debug("Find Max Order Page for api name : {}", apiName);
final Integer maxPageOrder = pageRepository.findMaxPageOrderByApi(apiName);
return maxPageOrder == null ? 0 : maxPageOrder;
} catch (TechnicalException ex) {
LOGGER.error("An error occured when searching max order page for api name [{}]", apiName, ex);
throw new TechnicalManagementException("An error occured when searching max order page for api name " + apiName, ex);
}
}
private PageListItem reduce(Page page) {
PageListItem pageItem = new PageListItem();
pageItem.setId(page.getId());
pageItem.setName(page.getName());
pageItem.setType(PageType.valueOf(page.getType().toString()));
pageItem.setOrder(page.getOrder());
pageItem.setLastContributor(page.getLastContributor());
pageItem.setPublished(page.isPublished());
pageItem.setSource(convert(page.getSource()));
pageItem.setConfiguration(convert(page.getConfiguration()));
return pageItem;
}
private static Page convert(NewPageEntity newPageEntity) {
Page page = new Page();
page.setName(newPageEntity.getName());
final PageType type = newPageEntity.getType();
if (type != null) {
page.setType(io.gravitee.repository.management.model.PageType.valueOf(type.name()));
}
page.setContent(newPageEntity.getContent());
page.setLastContributor(newPageEntity.getLastContributor());
page.setOrder(newPageEntity.getOrder());
page.setSource(convert(newPageEntity.getSource()));
page.setConfiguration(convert(newPageEntity.getConfiguration()));
return page;
}
private static PageEntity convert(Page page) {
PageEntity pageEntity = new PageEntity();
pageEntity.setId(page.getId());
pageEntity.setName(page.getName());
if (page.getType() != null) {
pageEntity.setType(page.getType().toString());
}
pageEntity.setContent(page.getContent());
if (isJson(page.getContent())) {
pageEntity.setContentType(MediaType.APPLICATION_JSON);
} else {
// Yaml or RAML format ?
pageEntity.setContentType("text/yaml");
}
pageEntity.setLastContributor(page.getLastContributor());
pageEntity.setLastModificationDate(page.getUpdatedAt());
pageEntity.setOrder(page.getOrder());
pageEntity.setPublished(page.isPublished());
if (page.getSource() != null) {
pageEntity.setSource(convert(page.getSource()));
}
if (page.getConfiguration() != null) {
pageEntity.setConfiguration(convert(page.getConfiguration()));
}
return pageEntity;
}
private static Page convert(UpdatePageEntity updatePageEntity) {
Page page = new Page();
page.setName(updatePageEntity.getName());
page.setContent(updatePageEntity.getContent());
page.setLastContributor(updatePageEntity.getLastContributor());
page.setOrder(updatePageEntity.getOrder());
page.setPublished(updatePageEntity.isPublished());
page.setSource(convert(updatePageEntity.getSource()));
page.setConfiguration(convert(updatePageEntity.getConfiguration()));
return page;
}
private static PageSource convert(PageSourceEntity pageSourceEntity) {
PageSource source = null;
if (pageSourceEntity != null && pageSourceEntity.getType() != null && pageSourceEntity.getConfiguration() != null) {
source = new PageSource();
source.setType(pageSourceEntity.getType());
source.setConfiguration(pageSourceEntity.getConfiguration());
}
return source;
}
private static PageSourceEntity convert(PageSource pageSource) {
PageSourceEntity entity = null;
if (pageSource != null) {
entity = new PageSourceEntity();
entity.setType(pageSource.getType());
try {
entity.setConfiguration((new ObjectMapper()).readTree(pageSource.getConfiguration()));
} catch (IOException e) {
e.printStackTrace();
}
}
return entity;
}
private static PageConfiguration convert(PageConfigurationEntity pageConfigurationEntity){
PageConfiguration configuration = null;
if(pageConfigurationEntity != null) {
configuration = new PageConfiguration();
configuration.setTryIt(pageConfigurationEntity.isTryIt());
configuration.setTryItURL(pageConfigurationEntity.getTryItURL());
}
return configuration;
}
private static PageConfigurationEntity convert(PageConfiguration pageConfiguration){
PageConfigurationEntity configurationEntity = null;
if(pageConfiguration != null) {
configurationEntity = new PageConfigurationEntity();
configurationEntity.setTryIt(pageConfiguration.isTryIt());
configurationEntity.setTryItURL(pageConfiguration.getTryItURL());
}
return configurationEntity;
}
private static final Gson gson = new Gson();
public static boolean isJson(String content) {
try {
gson.fromJson(content, Object.class);
return true;
} catch(com.google.gson.JsonSyntaxException ex) {
return false;
}
}
}