1

I get a file in Alfresco, with link for example: http://localhost:8080/share/proxy/alfresco/workspace/SpacesStore/c9d187e8-aec5-4177-9587-a5b924e514cd/exemplo.pdf with XMLHttpRequest on javascript and I don't have problems. But i'm trying to do this on JAVA:

public static byte[] callURL(String myURL) {
        System.out.println("Requeted URL:" + myURL);
        StringBuilder sb = new StringBuilder();
        URLConnection urlConn = null;
        InputStreamReader in = null;
        try {
            URL url = new URL(myURL);

            urlConn = url.openConnection();
            if (urlConn != null)
                urlConn.setReadTimeout(60 * 1000);
            if (urlConn != null && urlConn.getInputStream() != null) {
                in = new InputStreamReader(urlConn.getInputStream(),
                        Charset.defaultCharset());
                BufferedReader bufferedReader = new BufferedReader(in);
                if (bufferedReader != null) {
                    int cp;
                    while ((cp = bufferedReader.read()) != -1) {
                        sb.append((char) cp);
                    }
                    bufferedReader.close();
                }
            }
            in.close();
        } catch (Exception e) {
            throw new RuntimeException("Exception while calling URL:" + myURL, e);
        }
        String sbString = sb.toString();
        System.out.println(sbString);
        byte[] b = sbString.getBytes(StandardCharsets.UTF_8); 
        return b;
    }

But this gives to me

401 unauthorized

. What is the problem? I need any authentication ? How can I solve this problem?

PRVS
  • 1,612
  • 4
  • 38
  • 75
  • 1
    If /share/proxy/alfresco/ works for you means you have an active Share connection. With Java you need to authenticate separately against Alfresco repository. – Teqnology Nov 21 '15 at 07:04
  • How can I do that? I dont understand. – PRVS Nov 21 '15 at 08:22
  • I don't want to give a username or password, I want to receive this parameters... Or another way. – PRVS Nov 21 '15 at 09:22

1 Answers1

1

I solved it! For people who may have the same problem, follow this tutorial. You have, inclusive, how to access the content.

Plain text:

ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
String content = reader.getContentString();

Binary data:

ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
InputStream originalInputStream = reader.getContentInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8; //1KiB buffer
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = originalInputStream.read(buffer)) > -1) {
    outputStream.write(buffer, 0, bytesRead);
}
originalInputStream.close();
byte[] binaryData = outputStream.toByteArray();
PRVS
  • 1,612
  • 4
  • 38
  • 75