cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to deploy the bpmn file using activiti rest api post

jaikrishna
Champ on-the-rise
Champ on-the-rise
Hi ,

I have been trying to deploy the file through activiti rest api post ..
But I am not able to do it ..
Please help me ..
I have attached the code ..

public class repo
{   
private static String REST_URI = "http://localhost:8080/activiti-rest/service/repository";

private static ClientResource getClientResource(String uri)
{
ClientResource resource = new ClientResource(uri);
resource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,"kermit", "kermit");
return resource;
}

public static void postDeployments() throws JSONException,IOException {   



String postdeploymnet = REST_URI+"/deployments";






Path path = Paths.get(".\\src\\main\\resources\\diagrams\\SimpleLeaveProcess.bpmn");
String name = "SimpleLeaveProcess.bpmn";
String originalFileName = "SimpleLeaveProcess.bpmn";
String contentType = "";
byte[] content = null;
try {
content = Files.readAllBytes(path);
} catch (final IOException e) {
}
MultipartFile result = new MockMultipartFile(name,originalFileName, contentType, content);


Representation response1 = getClientResource(postdeploymnet).post(result, MediaType.MULTIPART_FORM_DATA);
}
}
9 REPLIES 9

vasile_dirla
Star Contributor
Star Contributor
Hi jaikrishna,
Do you have a error in the log or any other relevant information which can help us understand what's happening?

jaikrishna
Champ on-the-rise
Champ on-the-rise
After the line "Representation response1 = getClientResource(postdeploymnet).post(result, MediaType.MULTIPART_FORM_DATA);" , nothing is getting executed ..
Am I doing the file conversion to multipart format right ? the way I am calling the rest api post , arguments is that all right ?
I don't know where I am making mistake .. Please help me .
If you have the code for file deploy through activiti post , please share that as well..
Please guide

jaikrishna
Champ on-the-rise
Champ on-the-rise
How do I add the username and password in the code ?
Can you please tell me how to do that in the same link which you had posted

jbarrez
Star Contributor
Star Contributor
That's standard basic auth … nothing fancy about that at all .So simply by setting the Authorization header correctly (  httpConn.setRequestProperty("Authorization", credentials):smileywink:

jaikrishna
Champ on-the-rise
Champ on-the-rise
I am getting status returned as 401 .. I used the same multipartutility class file which you had mentioned .. I had added the credentials in setrequestproperty .. What might be the reason ? My url is right .. And I am able to deploy properly using rest client as well .. Programmatically while doing getting the error .. Please help me ..

jaikrishna
Champ on-the-rise
Champ on-the-rise
any update on this ?

jbarrez
Star Contributor
Star Contributor
Can't say much without you actually posting what you have currently…. it should be pretty straight forward Java, nothing special about that.

paravena
Champ in-the-making
Champ in-the-making
This works for me:

import org.apache.commons.codec.binary.Base64;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.FileSystemResource;
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.converter.FormHttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

public class DeployBarUtility2 {

    public static final String BAR_FILE_PATH = "/Users/paravena/dev/workspace/activiti-examples/add-sales-opportunity-process/target/add-sales-opportunity-process-1.0-SNAPSHOT.bar";
    public static final String DEPLOY_URL = "http://localhost:8080/activiti-rest/service/repository/deployments";

    public static void main(String[] args) throws Exception {
        new DeployBarUtility2().deploy(BAR_FILE_PATH);
    }

    public void deploy(String barFilePath) {
        RestTemplate restTemplate = getRestTemplate();

        MultiValueMap<String, Object> multipartDataMap = new LinkedMultiValueMap<String, Object>();

        FileSystemResource barFileResource = new FileSystemResource(barFilePath);
        multipartDataMap.add("file", barFileResource);

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(multipartDataMap, getHttpHeaders());


        ResponseEntity<Map<String, String>> response = restTemplate.exchange(DEPLOY_URL,
                HttpMethod.POST, requestEntity, new ParameterizedTypeReference<Map<String, String>>() {
                });

        System.out.println("Status: " + response.getStatusCode());
        System.out.println("Name: " + response.getBody().get("name"));
    }

    private RestTemplate getRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setMessageConverters(Arrays.asList(new FormHttpMessageConverter(),
                new ResourceHttpMessageConverter(),
                new MappingJackson2HttpMessageConverter()));
        return restTemplate;
    }

    private HttpHeaders getHttpHeaders() {
        HttpHeaders headers = new HttpHeaders();
        String token="kermit".concat(":").concat("kermit");
        headers.add("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        return headers;
    }
}