View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.console.rest;
20  
21  import java.io.InputStream;
22  import java.util.List;
23  import javax.ws.rs.core.HttpHeaders;
24  import javax.ws.rs.core.MediaType;
25  import javax.ws.rs.core.Response;
26  import org.apache.cxf.helpers.IOUtils;
27  import org.apache.cxf.jaxrs.client.WebClient;
28  import org.apache.cxf.jaxrs.impl.MetadataMap;
29  import org.apache.syncope.common.lib.to.BpmnProcess;
30  import org.apache.syncope.common.rest.api.RESTHeaders;
31  import org.apache.syncope.common.rest.api.service.BpmnProcessService;
32  
33  public class BpmnProcessRestClient extends BaseRestClient {
34  
35      private static final long serialVersionUID = 5049285686167071017L;
36  
37      public List<BpmnProcess> getDefinitions() {
38          return getService(BpmnProcessService.class).list();
39      }
40  
41      private BpmnProcessService getService(final MediaType mediaType) {
42          BpmnProcessService service = getService(BpmnProcessService.class);
43  
44          MetadataMap<String, String> headers = new MetadataMap<>();
45          headers.put(HttpHeaders.CONTENT_TYPE, List.of(mediaType.toString()));
46          headers.put(HttpHeaders.ACCEPT, List.of(mediaType.toString()));
47          WebClient.client(service).headers(headers);
48  
49          return service;
50      }
51  
52      public InputStream getDefinition(final MediaType mediaType, final String key) {
53          Response response = getService(mediaType).get(key);
54          resetClient(BpmnProcessService.class);
55  
56          return (InputStream) response.getEntity();
57      }
58  
59      public void setDefinition(final MediaType mediaType, final String key, final String definition) {
60          getService(mediaType).set(key, definition);
61          resetClient(BpmnProcessService.class);
62      }
63  
64      public byte[] getDiagram(final String key) {
65          BpmnProcessService service = getService(BpmnProcessService.class);
66          WebClient.client(service).accept(RESTHeaders.MEDIATYPE_IMAGE_PNG);
67          Response response = service.exportDiagram(key);
68  
69          byte[] diagram;
70          try {
71              diagram = IOUtils.readBytesFromStream((InputStream) response.getEntity());
72          } catch (Exception e) {
73              LOG.error("Could not get workflow diagram", e);
74              diagram = new byte[0];
75          }
76          return diagram;
77      }
78  
79      public void deleteDefinition(final String key) {
80          getService(BpmnProcessService.class).delete(key);
81      }
82  }