Skip to main content
Omnitracs Knowledge Base

uploadFile

uploadFile

The file upload interface supports File Upload via HTTP Post for Integration Customers.  

The authentication credentials are passed in as a part of the URL:
https://mm.myqualcomm.com/mms/upload/uploadfileWs/ssoCompanyID=COMPANYX/username=USERNAME/password=PASSWORD


Additional inputs supplied in the POST request are file, category and description and are passed as part of MultiPart request. See example code below.
Category and description are optional, however when category is omitted, the file will be placed into the company default category.

The response of the upload request is provided in form of XML object as in the examples below.

You will use the name of the file uploaded to reference the file in the createDelivery web service call.

 

Sample XML Results

A successful call will return a restResult as follows. A code of 0 indicates success.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:restResult xmlns:ns2="http://www.qualcomm.com/qes/mms">
      <code>0</code>
      <command>UPLOAD</command>
</ns2:restResult>

If there is an error encountered, the restResult will contain the reason and the code will be set to 1.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:restResult xmlns:ns2="http://www.qualcomm.com/qes/mms">
      <code>1</code>
      <command>UPLOAD</command>
      <reason>File with uploaded name already exists</reason>
      <result>File Upload Error</result>
</ns2:restResult>

 

Sample File Upload Code (Java)

Upload is accomplished using DefaultHttpClient.

public void testUploadFile()
  {
    DefaultHttpClient httpclient = (DefaultHttpClient) WebClientDevWrapper.wrapClient(new DefaultHttpClient());
 
    String filePath = this.testSourceDirectory + File.separatorChar + "test.pdf";
 
    try
    {
      HttpPost httppost = createHttpPost(filePath, "username", "password", "company");
 
      HttpResponse response = httpclient.execute(httppost);
      this.validateResponse(response.getEntity(), false);
 
      int statusCode = response.getStatusLine().getStatusCode();
      assertEquals(200, statusCode);
 
    }
    catch (Exception e)
    {
      fail("Caught Exception: " + e);
    }
    finally
    {
      httpclient.getConnectionManager().shutdown();
    }
  }
 
  private HttpPost createHttpPost(String filePath, String userName, String password, String companyName) throws UnsupportedEncodingException
  {
    FileBody bin = new FileBody(new File(filePath));
    StringBody description;
 
    description = new StringBody("A binary file of some kind");
 
    StringBody category = new StringBody("My Category");
 
    MultipartEntity requestEntity = new MultipartEntity();
    requestEntity.addPart("file", bin);
    requestEntity.addPart("description", description);
    requestEntity.addPart("category", category);
 
    HttpPost httppost = new HttpPost("https://mm.myqualcomm.com/mms/upload/uploadfileWs/ssoCompanyID=" + companyName
        + "/username=" + userName + "/password="+password);
 
    httppost.setEntity(requestEntity);
    return httppost;
  }
  
  private void validateResponse(HttpEntity resEntity, boolean expectSuccess) throws IOException, ParserConfigurationException,
      SAXException
  {
    String xmlString = EntityUtils.toString(resEntity);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = factory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(xmlString));
    Document doc = db.parse(inStream);
    String code = null;
    String result = null;
    String reason = null;
 
    Element root = doc.getDocumentElement();
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
    {
      if (nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
      {
        org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i);
        if (nameElement.getNodeName().equals("code"))
          code = nameElement.getFirstChild().getNodeValue().trim();
        if (nameElement.getNodeName().equals("result"))
          result = nameElement.getFirstChild().getNodeValue().trim();
        if (nameElement.getNodeName().equals("reason"))
          reason = nameElement.getFirstChild().getNodeValue().trim();
      }
    }
    // Always will have a code
    assertNotNull(code);
    if (expectSuccess)
    {
      assertTrue(Integer.valueOf(code).intValue() == 0);
      assertNull(reason);
      assertNull(result);
    }
    else
    {
      assertTrue(Integer.valueOf(code).intValue() > 0);
      assertNotNull(reason);
      assertNotNull(result);
      System.out.println("Reason: " + reason);
      System.out.println("Result: " + result);
 
    }
  }
  
 
 
WebClientDevWrapper.java
 
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
 
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
 
/*
 This code is public domain: you are free to use, link and/or modify it in any way you want, for all purposes including commercial applications.
 */
public class WebClientDevWrapper
{
 
  public static HttpClient wrapClient(HttpClient base)
  {
    try
    {
      SSLContext ctx = SSLContext.getInstance("TLS");
      X509TrustManager tm = new X509TrustManager()
      {
 
        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException
        {
        }
 
        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException
        {
        }
 
        public X509Certificate[] getAcceptedIssuers()
        {
          return null;
        }
      };
 
      ctx.init(null, new TrustManager[] { tm }, null);
      SSLSocketFactory ssf = new SSLSocketFactory(ctx);
      ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      ClientConnectionManager ccm = base.getConnectionManager();
      SchemeRegistry sr = ccm.getSchemeRegistry();
      sr.register(new Scheme("https", ssf, 443));
      return new DefaultHttpClient(ccm, base.getParams());
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
      return null;
    }
  }
}
  • Was this article helpful?