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.enduser.rest;
20  
21  import java.io.InputStream;
22  import java.util.List;
23  import javax.ws.rs.core.MediaType;
24  import javax.ws.rs.core.Response;
25  import org.apache.cxf.helpers.IOUtils;
26  import org.apache.cxf.jaxrs.client.Client;
27  import org.apache.cxf.jaxrs.client.WebClient;
28  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
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      private BpmnProcessService getService(final MediaType mediaType) {
38          BpmnProcessService service = getService(BpmnProcessService.class);
39          Client client = WebClient.client(service);
40          client.type(mediaType);
41          return service;
42      }
43  
44      public List<BpmnProcess> getDefinitions() {
45          return getService(BpmnProcessService.class).list();
46      }
47  
48      public InputStream getDefinition(final MediaType mediaType, final String key) {
49          Response response = getService(mediaType).get(key);
50          SyncopeEnduserSession.get().resetClient(BpmnProcessService.class);
51  
52          return (InputStream) response.getEntity();
53      }
54  
55      public byte[] getDiagram(final String key) {
56          BpmnProcessService service = getService(BpmnProcessService.class);
57          WebClient.client(service).accept(RESTHeaders.MEDIATYPE_IMAGE_PNG);
58          Response response = service.exportDiagram(key);
59  
60          byte[] diagram;
61          try {
62              diagram = IOUtils.readBytesFromStream((InputStream) response.getEntity());
63          } catch (Exception e) {
64              LOG.error("Could not get workflow diagram", e);
65              diagram = new byte[0];
66          }
67          return diagram;
68      }
69  }