Subversion Repositories linid-dm

Compare Revisions

Rev 500 → Rev 501

/branches/2.1/src/main/java/org/linid/dm/wui/t5lib/services/impl/StringConcatenationUidResolver.java New file
0,0 → 1,63
/* *********************************************************
* This file is a part of LinID Directory Manager.
* This software is a computer program whose purpose is
* to manage electronic identity thanks to LDAP directories.
*
* ==LICENSE NOTICE==
* LinID Directory Manager is a free software subjected to the
* ** GNU Affero Public License ** as published by the
* Free Software Foundation, ** version 3 ** of the license.
*
* By application to section 7 in the GNU Affero GPLv3,
* dynamic and static links do not extend license to other
* softwares.
*
* You can redistribute and/or modify since you respect
* the term of the license.
*
* NOTICE : THIS LICENSE IS FREE OF CHARGE AND THE SOFTWARE
* IS DISTRIBUTED WITHOUT ANY WARRANTIES OF ANY KIND
*
* ==LICENSE NOTICE==
*
* (c) 2008-2013 Linagora - http://linagora.com/
********************************************************* */
package org.linid.dm.wui.t5lib.services.impl;
 
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linid.dm.wui.t5lib.services.UidToDnResolver;
 
public class StringConcatenationUidResolver implements UidToDnResolver {
public static final String DEFAULT_UID_ATTRIBUTE = "uid";
private static final Log logger = LogFactory.getLog(StringConcatenationUidResolver.class);
private String branch;
private String uidAttribute;
public StringConcatenationUidResolver(String branch, String uidAttribute) {
this.branch = branch;
this.uidAttribute = uidAttribute;
}
public StringConcatenationUidResolver(String branch) {
this(branch, DEFAULT_UID_ATTRIBUTE);
}
 
@Override
public LdapName resolve(String uid) {
String dn = uidAttribute + "=" + uid + "," + branch;
try {
return new LdapName(dn);
} catch (InvalidNameException e) {
logger.error("Invalid DN : " + dn);
throw new RuntimeException("Invalid DN : " + dn, e);
}
}
 
}
/branches/2.1/src/main/java/org/linid/dm/wui/t5lib/services/impl/LdapJpegPhotoService.java New file
0,0 → 1,180
/* *********************************************************
* This file is a part of LinID Directory Manager.
* This software is a computer program whose purpose is
* to manage electronic identity thanks to LDAP directories.
*
* ==LICENSE NOTICE==
* LinID Directory Manager is a free software subjected to the
* ** GNU Affero Public License ** as published by the
* Free Software Foundation, ** version 3 ** of the license.
*
* By application to section 7 in the GNU Affero GPLv3,
* dynamic and static links do not extend license to other
* softwares.
*
* You can redistribute and/or modify since you respect
* the term of the license.
*
* NOTICE : THIS LICENSE IS FREE OF CHARGE AND THE SOFTWARE
* IS DISTRIBUTED WITHOUT ANY WARRANTIES OF ANY KIND
*
* ==LICENSE NOTICE==
*
* (c) 2008-2013 Linagora - http://linagora.com/
********************************************************* */
package org.linid.dm.wui.t5lib.services.impl;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
 
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
 
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry5.upload.services.UploadedFile;
import org.linid.dm.authorization.AuthorizationException;
import org.linid.dm.ldap.UnknownAttributeException;
import org.linid.dm.ldap.objects.IELdapAttribute;
import org.linid.dm.ldap.objects.IELdapEntry;
import org.linid.dm.ldap.objects.impl.ByteString;
import org.linid.dm.ldap.objects.impl.ELdapAttribute;
import org.linid.dm.ldap.postprocessing.PostProcessingException;
import org.linid.dm.ldap.repository.LdapEntryRepository;
import org.linid.dm.wui.t5lib.services.PhotoStorageService;
import org.linid.dm.wui.t5lib.services.UidToDnResolver;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
 
public class LdapJpegPhotoService implements PhotoStorageService {
 
public static final String PHOTO_ATTRIBUTE_ID = "jpegPhoto";
 
private static final Log logger = LogFactory.getLog(LdapJpegPhotoService.class);
private static final String[] SEARCH_ATTRS = new String[] {"objectClass", PHOTO_ATTRIBUTE_ID};
private LdapEntryRepository ldapEntryRepository;
private UidToDnResolver uidResolver;
 
public LdapJpegPhotoService(LdapEntryRepository ldapEntryRepository, UidToDnResolver uidResolver) {
this.ldapEntryRepository = ldapEntryRepository;
this.uidResolver = uidResolver;
}
 
@Override
public void store(String uid, UploadedFile file) {
IELdapEntry entry = getEntry(uid);
addPhotoAttribute(entry, file);
updateEntry(entry);
}
 
private IELdapEntry getEntry(String uid) {
IELdapEntry entry = null;
LdapName dn = uidResolver.resolve(uid);
try {
entry = ldapEntryRepository.get(getPrincipal(),
dn,
SEARCH_ATTRS);
} catch (AuthorizationException e) {
String errorString = String.format("Access not allowed to read DN: %s (uid: %s)", dn, uid);
logger.error(errorString);
throw new RuntimeException(errorString, e);
}
return entry;
}
 
private void addPhotoAttribute(IELdapEntry entry, UploadedFile file) {
IELdapAttribute photoAttribute = new ELdapAttribute(PHOTO_ATTRIBUTE_ID);
try {
ByteString byteString = new ByteString();
byteString.setEncoding("FAKE_BINARY");
byteString.setValue(IOUtils.toByteArray(file.getStream()));
photoAttribute.setValue(byteString);
entry.setAttribute(photoAttribute);
} catch (IOException e) {
String errorString = "Can't read string from uploaded file";
logger.error(errorString);
throw new RuntimeException(errorString, e);
} catch (UnknownAttributeException e) {
String errorString = "Unknown attribute " + PHOTO_ATTRIBUTE_ID;
logger.error(errorString);
throw new RuntimeException(errorString, e);
}
}
 
private void updateEntry(IELdapEntry entry) {
try {
ldapEntryRepository.update(getPrincipal(), entry);
} catch (AuthorizationException e) {
String errorString = String.format("Access not allowed to write attribute %s for DN: %s", PHOTO_ATTRIBUTE_ID, entry.getDn().toString());
logger.error(errorString);
throw new RuntimeException(errorString, e);
} catch (PostProcessingException e) {
String errorString = String.format("Post processing exception while writing attribute %s for DN: %s", PHOTO_ATTRIBUTE_ID, entry.getDn().toString());
logger.error(errorString);
throw new RuntimeException(errorString, e);
}
}
 
@Override
public void delete(String uid) {
IELdapEntry entry = getEntry(uid);
IELdapAttribute photoAttribute = entry.getAttribute(PHOTO_ATTRIBUTE_ID);
if (photoAttribute != null) {
entry.removeAttribute(PHOTO_ATTRIBUTE_ID);
}
updateEntry(entry);
}
 
@Override
public void rename(String oldUid, String newUid) {
// Nothing to do
}
@Override
public InputStream get(String uid) {
IELdapEntry entry = getEntry(uid);
return photoAttributeToStream(entry);
}
 
private InputStream photoAttributeToStream(IELdapEntry entry) {
IELdapAttribute photoAttribute = entry.getAttribute(PHOTO_ATTRIBUTE_ID);
if (photoAttribute == null) {
return null;
}
byte[] value = photoAttribute.getValue().value();
ByteArrayInputStream stream = new ByteArrayInputStream(value);
return stream;
}
 
private String getPrincipalDn() {
LdapUserDetails tapestryPrincipal = null;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
 
if (authentication != null) {
Object acegiPrincipal = authentication.getPrincipal();
if (acegiPrincipal != null && acegiPrincipal instanceof LdapUserDetails) {
tapestryPrincipal = (LdapUserDetails) acegiPrincipal;
return tapestryPrincipal.getDn();
}
}
return null;
}
private LdapName getPrincipal() {
try {
return new LdapName(getPrincipalDn());
} catch (InvalidNameException e) {
String errorString = "Invalid DN";
logger.error(errorString);
throw new RuntimeException(errorString, e);
}
}
 
 
}
/branches/2.1/src/main/java/org/linid/dm/wui/t5lib/services/UidToDnResolver.java New file
0,0 → 1,32
/* *********************************************************
* This file is a part of LinID Directory Manager.
* This software is a computer program whose purpose is
* to manage electronic identity thanks to LDAP directories.
*
* ==LICENSE NOTICE==
* LinID Directory Manager is a free software subjected to the
* ** GNU Affero Public License ** as published by the
* Free Software Foundation, ** version 3 ** of the license.
*
* By application to section 7 in the GNU Affero GPLv3,
* dynamic and static links do not extend license to other
* softwares.
*
* You can redistribute and/or modify since you respect
* the term of the license.
*
* NOTICE : THIS LICENSE IS FREE OF CHARGE AND THE SOFTWARE
* IS DISTRIBUTED WITHOUT ANY WARRANTIES OF ANY KIND
*
* ==LICENSE NOTICE==
*
* (c) 2008-2013 Linagora - http://linagora.com/
********************************************************* */
package org.linid.dm.wui.t5lib.services;
 
import javax.naming.ldap.LdapName;
 
public interface UidToDnResolver {
LdapName resolve(String uid);
}
branches/2.1/src/main/java/org/linid/dm/wui/t5lib/services/UidToDnResolver.java Property changes : Added: svn:mime-type + text/plain