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.resources;
20  
21  import java.io.IOException;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.ws.rs.core.MediaType;
24  import javax.ws.rs.core.Response;
25  import org.apache.cxf.common.util.UrlUtils;
26  import org.apache.syncope.client.console.rest.BpmnProcessRestClient;
27  import org.apache.syncope.client.ui.commons.annotations.Resource;
28  import org.apache.syncope.common.lib.to.BpmnProcess;
29  import org.apache.wicket.util.io.IOUtils;
30  
31  /**
32   * Mirror REST resource for setting BPMN process in JSON (used by Flowable Modeler).
33   */
34  @Resource(key = "bpmnProcessPUT", path = "/bpmnProcessPUT")
35  public class BpmnProcessPUTResource extends AbstractBpmnProcessResource {
36  
37      private static final long serialVersionUID = 2964542005207297944L;
38  
39      public BpmnProcessPUTResource(final BpmnProcessRestClient bpmnProcessRestClient) {
40          super(bpmnProcessRestClient);
41      }
42  
43      @Override
44      protected ResourceResponse newResourceResponse(final Attributes attributes) {
45          String definition = null;
46          try {
47              HttpServletRequest request = (HttpServletRequest) attributes.getRequest().getContainerRequest();
48              String requestBody = IOUtils.toString(request.getInputStream());
49              String[] split = requestBody.split("&");
50              for (int i = 0; i < split.length && definition == null; i++) {
51                  String keyValue = split[i];
52                  if (keyValue.startsWith("json_xml=")) {
53                      definition = UrlUtils.urlDecode(keyValue.split("=")[1]);
54                  }
55              }
56          } catch (IOException e) {
57              LOG.error("Could not extract BPMN process", e);
58          }
59  
60          BpmnProcess toSet = getBpmnProcess(attributes);
61  
62          if (definition == null || toSet == null) {
63              return new ResourceResponse().setStatusCode(Response.Status.BAD_REQUEST.getStatusCode()).
64                      setError(Response.Status.BAD_REQUEST.getStatusCode(),
65                              "Could not extract BPMN process' model id and / or definition");
66          }
67  
68          try {
69              bpmnProcessRestClient.setDefinition(MediaType.APPLICATION_JSON_TYPE, toSet.getKey(), definition);
70              return new ResourceResponse().setStatusCode(Response.Status.NO_CONTENT.getStatusCode());
71          } catch (Exception e) {
72              LOG.error("While updating BPMN process", e);
73              return new ResourceResponse().setStatusCode(Response.Status.BAD_REQUEST.getStatusCode()).
74                      setError(Response.Status.BAD_REQUEST.getStatusCode(),
75                              "While updating BPMN process: " + e.getMessage());
76          }
77      }
78  }