cancel
Showing results for 
Search instead for 
Did you mean: 

Deployment using Spring RestTemplate

jindal8787
Champ in-the-making
Champ in-the-making
Hi Team

We are trying to write Spring RestTemplate client for Activiti REST's Create Deployment service to deploy our BPMN. Any ready code in Spring RestTemplate would be really helpful.

I'm trying below example but it is throwing 400 BAD Request -

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Objects;

import org.apache.tomcat.util.codec.binary.Base64;
import org.json.JSONObject;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

public class ActivitiRESTDeployerSpring1 {

  private static final String TARGET_URL =
      "http://10.99.25.2:8090/activiti-rest/service/repository/deployments";

  public ActivitiRESTDeployerSpring1() {}

  private void deploy() {

    RestTemplate restTemplate = new RestTemplate(getAuthenticatedHeader());
    String tenantId = "myTenant";

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(new MediaType("multipart", "form-data"));

    Resource file =
        new FileSystemResource(
            "C:\\Users\\jindal.a\\Desktop\\DeploymentRestSample\\SampleProject\\deployment\\SampleICDExceptionReportWorkflow.bar");
    MultiValueMap<String, Object> multipartMap =
        new LinkedMultiValueMap<String, Object>();
    multipartMap.add("file", file);
    multipartMap.add("tenantId", tenantId);

    HttpEntity<MultiValueMap<String, Object>> requestEntity =
        new HttpEntity<MultiValueMap<String, Object>>(multipartMap, headers);

    ResponseEntity<DeploymentResponse> responseEntity = null;
    try {
      responseEntity =
          restTemplate.exchange(TARGET_URL, HttpMethod.POST, requestEntity,
              DeploymentResponse.class);
    } catch (HttpClientErrorException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

      JSONObject obj = new JSONObject(e.getResponseBodyAsString());

      System.out.println(obj);
    }

    if (!Objects.isNull(responseEntity)
        && !Objects.isNull(responseEntity.getBody())) {
      System.out.println("Deployment Done with deployment ID – "
          + responseEntity.getBody().getId());
    }

  }

  public static void main(String[] args) {
    new ActivitiRESTDeployerSpring1().deploy();

  }

  public SimpleClientHttpRequestFactory getAuthenticatedHeader() {
    SimpleClientHttpRequestFactory connectionFactory =
        new SimpleClientHttpRequestFactory() {
          @Override
          protected void prepareConnection(final HttpURLConnection connection,
              final String httpMethod) throws IOException {
            super.prepareConnection(connection, httpMethod);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);

            String authorisation = "kermit:kermit";
            byte[] encodedAuthorisation =
                Base64.encodeBase64(authorisation.getBytes());
            connection.setRequestProperty("Authorization", "Basic "
                + new String(encodedAuthorisation));
            connection.setRequestProperty("Content-Type",
                MediaType.APPLICATION_JSON_VALUE);
            connection.setRequestProperty("Accept",
                MediaType.APPLICATION_JSON_VALUE);
          }
        };
    return connectionFactory;
  }
}
4 REPLIES 4

trademak
Star Contributor
Star Contributor
Hi Amit,

We have unit tests that deploy process definitions to the Activiti Engine via a REST service, so maybe that's a good place to look for some code examples.

Best regards,

jindal8787
Champ in-the-making
Champ in-the-making
Thanks trademark! Could you please share the code/link?

jbarrez
Star Contributor
Star Contributor

jindal8787
Champ in-the-making
Champ in-the-making
Thanks jbarrez!