Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Cesar Quinteiro
How to send Bytes as a JSON string
February 9, 2013 10:27 AM
Answer

Cesar Quinteiro

Rank: Junior Member

Posts: 29

Join Date: October 18, 2011

Recent Posts

I was able to create a DlFileEntry , with the following code: But it works only with empty “bytes” you get a record create but with a cero bytes, I try many diferents ways to convert the bytes into Json string, but any works I always get the error:

{"exception":"Unable to convert to type: [B\njodd.typeconverter.TypeConversionException: Unable to convert value: JVBERi0xLjQKJeLjz9MKNSAwIG9iago8


what is the correct way to fill up the param “bytes” in a /dlapp/add-file-entry

Here is the code I was trying :

 1    public static void main(String[] args) throws Exception {
 2        long folderId =  13713;
 3        long groupId = 10179;
 4        String scopeGroupId ="10179";
 5        String mimeType =  "application/pdf";
 6        long fileEntryTypeId = 0;
 7
 8
 9        File file = new File("temp"+"sourceFileName");
10        String url = "http://localhost:8080/pdfs/ACTANum126.pdf";
11        System.err.println("URL A conectar" +  url);
12        URLConnection conn;
13        try {
14            conn = new URL(url).openConnection();
15
16        conn.connect();
17        InputStream is = conn.getInputStream();
18        byte[] bytes = IOUtils.toByteArray(is);
19
20
21        Base64 base64 = new Base64();
22        byte[] buffer = new byte[(int) file.length() + 100];
23        int length = is.read(buffer);
24        String sTRBytes = Base64.encodeBase64URLSafeString(bytes);
25//        String sTRBytes = JsonWriter.objectToJson(bytes);
26//        String sTRBytes = JsonWriter.toJson(bytes);
27
28        is.close();
29        addDLFileEntry(sTRgroupId, sTRfolderId , "titlessaasss", "description", sTRfileEntryTypeId , sTRfile, sTRis , sTRsize, sTRscopeGroupId, mimeType,sTRBytes );
30
31public static void addDLFileEntry(String groupId, String folderId, String title, String description, String fileEntryTypeId, String file, String is, String size, String scopeGroupId, String mimeType,String sTRBytes) throws Exception {
32   
33   
34    HttpHost targetHost = new HttpHost("localhost",8080,"http");
35    DefaultHttpClient httpclient = new DefaultHttpClient();
36    httpclient.getCredentialsProvider().setCredentials(
37            new AuthScope(targetHost.getHostName(), targetHost.getPort()),
38            new UsernamePasswordCredentials("test@liferay.com", "brasin"));
39
40    // Create AuthCache instance
41    AuthCache authCache = new BasicAuthCache();
42    // Generate BASIC scheme object and add it to the local
43    // auth cache
44    BasicScheme basicAuth = new BasicScheme();
45    authCache.put(targetHost, basicAuth);
46
47    // Add AuthCache to the execution context
48    BasicHttpContext ctx = new BasicHttpContext();
49    ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);
50    HttpPost post = new HttpPost("/api/secure/jsonws/dlapp/add-file-entry");
51    List<NameValuePair> params = new ArrayList<NameValuePair>();
52
53    params.add(new BasicNameValuePair("repositoryId", "10179"));
54    params.add(new BasicNameValuePair("folderId",folderId));
55    params.add(new BasicNameValuePair("sourceFileName", "/Applications/Liferay/liferay-portal-6.1.1-ce-ga2/tomcat-7.0.27/webapps/pdfs/ACTANum126.pdf"));
56    params.add(new BasicNameValuePair("mimeType", mimeType));
57   
58    params.add(new BasicNameValuePair("title", title));
59    params.add(new BasicNameValuePair("description", "description"));
60    params.add(new BasicNameValuePair("changeLog", "file"));
61    params.add(new BasicNameValuePair("bytes", sTRBytes));
62    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
63    post.setEntity(entity);
64    HttpResponse resp = httpclient.execute(targetHost, post, ctx);
65    System.out.println(resp.getStatusLine());
66    resp.getEntity().writeTo(System.out);
67    httpclient.getConnectionManager().shutdown();
68
69}
Cesar Quinteiro
RE: How to send Bytes as a JSON string
February 9, 2013 12:05 PM
Answer

Cesar Quinteiro

Rank: Junior Member

Posts: 29

Join Date: October 18, 2011

Recent Posts

Finally I found the solution:

what the param need is just a list of values that represents the data by example : 23,34,35,12….
as String sTRBytes = JsonWriter.objectToJson(bytes); returns

{"@type":"B","@items”:[23,34,35,12…..]}

you just have to take off what is not need

sTRBytes = sTRBytes.substring(24,sTRBytes.length()-2);

(or any other way to get the content of a Byte[] as a string of a list of numbers)

then with :
params.add(new BasicNameValuePair("bytes", sTRBytes));
you will your data upload a attached to the DLFileEntry Row