AnsweredAssumed Answered

Send Attachment

Question asked by fedemori on Aug 7, 2013
Latest reply on Aug 12, 2013 by frederikheremans1
Hi,
I'm creating a client to connect to Activiti with REST.
I logged in correctly and start the process with variables but I can't send an attachment.
Can you help me? This is my code.


HttpResponse response = null;
      String processInstanceId = null;      
      String taskDefinitionKey = null;
      String taskId = null;
      
      CryptUtil crypt = CryptUtil.getInstance();
      String pwdCrypt = crypt.encrypt(password);
      DefaultHttpClient client = new DefaultHttpClient();
      client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080),
          new UsernamePasswordCredentials(user, pwdCrypt));

      // Create AuthCache instance
      AuthCache authCache = new BasicAuthCache();
      // Generate BASIC scheme object and add it to the local auth cache
      BasicScheme basicAuth = new BasicScheme();
      authCache.put(new HttpHost("localhost", 8080, "http"), basicAuth);

      // Add AuthCache to the execution context
      BasicHttpContext localcontext = new BasicHttpContext();
      localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
         
      try {
         HttpPost postMethod = new HttpPost("http://localhost:8080/activiti-rest/service/process-instance");
         LoadProp lprop = new LoadProp();
         String procUOIA = lprop.getProcKeyUOIA();
         String groupUOIA = lprop.getGroupUOIA();
         taskDefinitionKey = lprop.getTaskDefinitionKey();
                  
         Format formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
         String sData = formatter.format(data);
         
         JSONObject jsonObj = new JSONObject();
         jsonObj.put("processDefinitionId", procUOIA);
         jsonObj.put("mittente", mittente);   
         jsonObj.put("oggetto", oggetto);   
         jsonObj.put("data", sData);   
         jsonObj.put("note", note);
         jsonObj.put("group", groupUOIA);
         //postMethod.setEntity(new StringEntity("{\"processDefinitionId\":\"" + procUOIA + "\"}", "UTF-8"));
                     
         StringEntity se = new StringEntity(jsonObj.toString(),"UTF-8"); 
         se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
         postMethod.setEntity(se);      
         response = client.execute(postMethod, localcontext);
         
      } catch (Exception e) {
             e.printStackTrace();       }
      try {
         //System.out.println(IOUtils.toString(response.getEntity().getContent()));
         HttpEntity httpEntity = response.getEntity();
           InputStream is = httpEntity.getContent();      
          
           BufferedReader reader = new BufferedReader(new InputStreamReader(
                   is, "iso-8859-1"), 8);
           StringBuilder sb = new StringBuilder();
           String line = null;
           while ((line = reader.readLine()) != null) {
               sb.append(line + "\n");
           }
           is.close();
           String json = sb.toString();
      
          JSONObject jObj = new JSONObject(json);
          processInstanceId = jObj.getString("processInstanceId");
       } catch (Exception e) {
             e.printStackTrace       }      
      
      try{
         HttpGet getMethod = new HttpGet("http://localhost:8080/activiti-rest/service/process-instance/" + processInstanceId);
         response = client.execute(getMethod, localcontext);
         
         HttpEntity httpEntity = response.getEntity();
           InputStream is = httpEntity.getContent();      
          
           BufferedReader reader = new BufferedReader(new InputStreamReader(
                   is, "iso-8859-1"), 8);
           StringBuilder sb = new StringBuilder();
           String line = null;
           while ((line = reader.readLine()) != null) {
               sb.append(line + "\n");
           }
           is.close();
           String json = sb.toString();
          
          JSONObject jObj = new JSONObject(json);
          if (jObj.get("tasks") != null && jObj.get("tasks").getClass().getName().equalsIgnoreCase("org.json.JSONArray")){
             JSONArray jArray = jObj.getJSONArray("tasks");
             for (int i = 0; i < jArray.length(); i++){
                JSONObject jso = jArray.getJSONObject(i);
                if(jso.getString("taskDefinitionKey").equalsIgnoreCase(taskDefinitionKey)){
                   taskId = jso.getString("taskId");
                   break;
                }
             }
          }
          if (taskId == null){
             risp.setEsito("KO");
            err.setDescrizione("Task del processo UOIA non trovato");
            err.setCodice("106");
            risp.setErrore(err);
            return risp;
          }
             
         
      }
      catch (Exception e) {
          risp.setEsito("KO");
         e.printStackTrace();       }   
      
      try{
         File test = new File("E:/lecce.doc");
         
         HttpPost postMethod =
               new HttpPost("http://localhost:8080/activiti-rest/service/task/" + taskId + "/attachment");
         
         InputStreamEntity reqEntity = new InputStreamEntity(
                 new FileInputStream(test), test.length());
         reqEntity.setContentType(FileUtil.getMimeType(test));
         reqEntity.setChunked(true);
         postMethod.setEntity(reqEntity);      
         response = client.execute(postMethod, localcontext);
         
         HttpEntity httpEntity = response.getEntity();
           InputStream is = httpEntity.getContent();      
          
           BufferedReader reader = new BufferedReader(new InputStreamReader(
                   is, "iso-8859-1"), 8);
           StringBuilder sb = new StringBuilder();
           String line = null;
           while ((line = reader.readLine()) != null) {
               sb.append(line + "\n");
           }
           is.close();
           String json = sb.toString();         
          JSONObject jObj = new JSONObject(json);
          System.out.println(jObj);
         
      }
      catch (Exception e) {
          e.printStackTrace();       }      
   

Outcomes