0

I have added a picture to a user through the OpenDJ Control Panel, but I can't seem to retrieve that picture so I can show it on a webpage. I have found two attributes thumbnailPicture and photo and tried accessing it the following way:

NamingEnumeration<?> namingEnum = ctx.search(baseFilter, "(uid="+username+")", searchControls);

                if (namingEnum.hasMore ()){
                    SearchResult result = (SearchResult) namingEnum.next ();    
                    Attributes attrs = result.getAttributes ();
                    photo=(String)attrs.get("thumbnailPicture").get() ;
                }

With the code is nothing wrong because I can retrieve an email adres for example when I change photo=(String)attrs.get("thumbnailPicture").get() ;

to

photo=(String)attrs.get("mail").get() ;

I noticed that when adding a picture through the OpenDJ Control Panel it goes via a browse button so I'm guessing the path should be stored somewhere?

I don't get any error messages (I presume because the attributes thumbnailPicture and photo aren't filled in in OpenDJ so they return empty) but what I would like is:

  • the path to where the picture is saved but it's just a guess that OpenDJ actually stores this information?
  • the possibility to retrieve the picture but I'm not sure as what it is saved in OpenDJ. The code now is meant for the path of the file (that's why there's a cast to String).
Noosrep
  • 416
  • 10
  • 24
  • I am puzzled by your mention of "thumbnailPicture and photo" in the text, but the code shows "thumbnailPhoto"? – JnRouvignac Jul 31 '15 at 22:23
  • Also, you are using JNDI. Why are you not using the more modern OpenDJ SDK instead? – JnRouvignac Jul 31 '15 at 22:24
  • @JnRouvignac JNDI works, why wouldn't you use it? The LDAP protocol hasn't changed. – user207421 Jul 31 '15 at 22:53
  • JNDI works indeed but the API is not great. Code using it is clumsy and hard to read. – JnRouvignac Aug 02 '15 at 07:04
  • @JnRouvignac you're right, it's not going to work if I don't use the right attribute.. – Noosrep Aug 02 '15 at 17:26
  • @JnRouvignac That's not a reason to completely change an existing implementation, and it certainly isn't a solution to this problem, whatever it is. And against that, JNDI is already present in the JDK. – user207421 Aug 02 '15 at 19:10
  • I suggested an alternative. I never said to do it now. Nothing suggests whether this code is new or old. Although it does not address the problem, I thought this section was designed for on the side comments? – JnRouvignac Aug 03 '15 at 07:02

4 Answers4

1

I think you may need to ask for the jpegPhoto attribute?

JnRouvignac
  • 807
  • 5
  • 19
1

The directory doesn't store the path to the image. It stores the image.

You don't bother to mention what error message you get, or how far you really get into this code, which makes a definitive answer impossible, but photo attributes are returned as byte arrays, not Strings, so:

photo=(String)attrs.get("thumbnailPicture").get() ;

If the result didn't have such an attribute, attrs.get("thumbnailPicture") would return null, so the following .get() would throw a NullPointerException. If it did have such an attribute, and a value for it, you would get a ClassCastException from trying to cast byte[] to String. The only conclusion possible is that this line of code isn't being executed, so photo remains at its prior value, presumably null, which is then causing whatever behaviour you are observing, which you haven't told us despite numerous requests.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I don't get an error message, otherwise I would have added it! I need the path of the file so that should be a string right? – Noosrep Aug 02 '15 at 17:24
  • The content of the attribute is a binary picture, not a path. You still haven't stated what your actual problem is. – user207421 Aug 02 '15 at 19:07
  • Updated the question in the hope that it's clearer now – Noosrep Aug 03 '15 at 07:07
  • I've already answered all this. I can't account for why you get nothing, but the attribute contains the picture, in bytes, not the pathname. Is `thumbnailPicture` mentioned in the attributes-to-return in the `SearchControls`? I would have thought you'd get an NPE if there was really nothing there. Does the `attrs.get()` line really execute? – user207421 Aug 03 '15 at 07:47
  • Yes the line executes and the question still is knowing if there is a way to find out the path of the file. Does OpenDJ store it (since it uses the browse button). In my opinion this shouldn't even be an answer but as a comment to the initial question – Noosrep Aug 03 '15 at 09:59
  • The answer is still that the path of the file isn't in the directory. How many times exactly does this have to be reiterated? The answer is also that if you really executed `attrs.get()` on an entry that didn't have a `thumbnailPhoto` attribute at all, I would expect to get `null` from that, and an NPE from the following `get()`; if it did have such an attribute with a value, I would expect a `ClassCastException` from trying to cast `byte[]` to `String`. It is hard to believe your assertion that this line of code was executed, and it it was, you still haven't told us what it yielded. – user207421 Aug 03 '15 at 10:08
  • Your recent edit is the first time that you specified that the directory doesn't store the path so I wonder where you get the reiterated part. If the answer was like this the first time it was posted, we wouldn't have this unnecessary conversation. – Noosrep Aug 03 '15 at 10:32
  • Rubbish. I specified it three prior times in comments. It's up there in black and white: "The content of the attribute is a binary picture, not a path." "The attribute contains the picture, in bytes, not the pathname." "The path of the file isn't in the directory." You aren't even reading what's written here. That's why the conversation was unnecessary. – user207421 Aug 03 '15 at 10:38
  • Saying that the content of an attribute is a binary picture is not the same as saying that the directory doesn't contain the path! The third time was finally the specification that the path file isn't in the directory and that's the one I see as a helfpul comment/answer. You say that I'm not even reading what's written here, I suggest you should look at yourself in the future in regards to reading the initial question. – Noosrep Aug 03 '15 at 12:02
  • More rubbish. 'Not a path' is contained in my first comment above, among the several you have failed to read and have claimed don't exist. I can't help you if you don't understand plain English reiterated four times. – user207421 Aug 05 '15 at 11:27
  • And read my fifth comment below. We can go on like this forever but the truth is still going to be that the answer as it is now is sooo much better and clearer than the answer you gave before the numerous edits. Which wasn't even an answer to begin with.. Either way I'm going to give up, never have I seen someone so arrogant on this site and I'm not going to spend any more time with you. Feel free to have the last word, but may I suggest that you should choose rubbish. It fits you well – Noosrep Aug 06 '15 at 16:14
0

for user image, there are existed several attributes:

  1. thumbnailphoto (specially fro Active Directory)
  2. jpegPhoto
  3. and photo

so use following codes:

photo=(String)attrs.get("thumbnailPhoto").get() ;

or,

photo=(String)attrs.get("jpegPhoto").get() ;
M2E67
  • 937
  • 7
  • 23
0

I use below code to write thumbnailPhoto to File

    NamingEnumeration<SearchResult> results = ctx.search(base, filter, sc);
    // while (results.hasMore()) {
    SearchResult sr = (SearchResult) results.next();
    Attributes attrs = sr.getAttributes();
    Attribute attr = attrs.get("thumbnailPhoto"); 
  
    byte[] image =(byte[]) attr.get();       

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
   ObjectOutputStream out = new ObjectOutputStream(bos) ;
   out.writeObject(result);

    Path path = Paths.get("output.jpg");
    try {
        Files.write(path,  image);    // Java 7+ only
        System.out.println("Successfully written data to the file");
    }
    catch (IOException e) {
        e.printStackTrace();
    }