Search in sources :

Example 1 with ValueAndType

use of io.milton.http.values.ValueAndType in project lobcder by skoulouzis.

the class SearchRunnable method sendPersons.

private void sendPersons(int currentMessageId, String baseContext, Set<LdapContact> persons, Set<String> returningAttributes) throws IOException, NotAuthorizedException, BadRequestException {
    LogUtils.debug(log, "sendPersons", baseContext, "size:", persons.size());
    boolean needObjectClasses = returningAttributes.contains("objectclass") || returningAttributes.isEmpty();
    boolean returnAllAttributes = returningAttributes.isEmpty();
    if (persons.isEmpty()) {
        log.warn("No contacts to send! -------------------");
    }
    for (LdapContact person : persons) {
        if (abandon) {
            log.warn("Abandon flag is set, so exiting send!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            break;
        }
        Map<String, Object> response = new HashMap<String, Object>();
        Set<LdapMappedProp> props = propertyMapper.mapProperties(returnAllAttributes, returningAttributes, person);
        response.put("uid", person.getName());
        for (LdapMappedProp prop : props) {
            ValueAndType vt;
            try {
                vt = propertyMapper.getProperty(prop.mappedName, person);
            } catch (NotAuthorizedException ex) {
                vt = null;
            }
            if (vt == null) {
                LogUtils.trace(log, "sendPersons: property not found: ldap property: ", prop.ldapName, " - dav prop: ", prop.mappedName, "resource: ", person.getClass());
            } else {
                if (vt.getValue() != null) {
                    response.put(prop.ldapName, vt.getValue());
                }
            }
        }
        // Process all attributes which have static mappings
        for (Map.Entry<String, String> entry : Ldap.STATIC_ATTRIBUTE_MAP.entrySet()) {
            String ldapAttribute = entry.getKey();
            String value = entry.getValue();
            if (value != null && (returnAllAttributes || returningAttributes.contains(ldapAttribute))) {
                response.put(ldapAttribute, value);
            }
        }
        if (needObjectClasses) {
            response.put("objectClass", Ldap.PERSON_OBJECT_CLASSES);
        }
        // iCal: copy email to apple-generateduid, encode @
        if (returnAllAttributes || returningAttributes.contains("apple-generateduid")) {
            String mail = (String) response.get("mail");
            if (mail != null) {
                response.put("apple-generateduid", mail.replaceAll("@", "__AT__"));
            } else {
                // failover, should not happen
                // failover, should not happen
                response.put("apple-generateduid", response.get("uid"));
            }
        }
        // iCal: replace current user alias with login name
        if (user.getName().equals(response.get("uid"))) {
            if (returningAttributes.contains("uidnumber")) {
                response.put("uidnumber", user.getName());
            }
        }
        LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_SEND_PERSON", currentMessageId, response.get("uid"), baseContext, response);
        responseHandler.sendEntry(currentMessageId, "uid=" + response.get("uid") + baseContext, response);
    }
}
Also used : ValueAndType(io.milton.http.values.ValueAndType) LdapMappedProp(io.milton.ldap.LdapPropertyMapper.LdapMappedProp) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) LdapContact(io.milton.resource.LdapContact)

Example 2 with ValueAndType

use of io.milton.http.values.ValueAndType in project lobcder by skoulouzis.

the class PropFindXmlGeneratorHelper method sendProperties.

private void sendProperties(Response.Status status, XmlWriter writer, Map<String, String> mapOfNamespaces, Map<QName, ValueAndType> properties, String href) {
    if (!properties.isEmpty()) {
        XmlWriter.Element elPropStat = writer.begin(WebDavProtocol.NS_DAV.getPrefix(), "propstat").open();
        XmlWriter.Element elProp = writer.begin(WebDavProtocol.NS_DAV.getPrefix(), "prop").open();
        for (QName qname : properties.keySet()) {
            String prefix = mapOfNamespaces.get(qname.getNamespaceURI());
            ValueAndType val = properties.get(qname);
            valueWriters.writeValue(writer, qname, prefix, val, href, mapOfNamespaces);
        }
        elProp.close();
        writer.writeProperty(WebDavProtocol.NS_DAV.getPrefix(), "status", status.toString());
        elPropStat.close();
    }
}
Also used : ValueAndType(io.milton.http.values.ValueAndType) QName(javax.xml.namespace.QName) XmlWriter(io.milton.http.XmlWriter)

Example 3 with ValueAndType

use of io.milton.http.values.ValueAndType in project lobcder by skoulouzis.

the class PropertySourcePatchSetter method setProperties.

@Override
public PropFindResponse setProperties(String href, ParseResult parseResult, Resource r) throws NotAuthorizedException, BadRequestException {
    log.trace("setProperties: resource type: {}", r.getClass());
    Map<QName, ValueAndType> knownProps = new HashMap<QName, ValueAndType>();
    Map<Status, List<NameAndError>> errorProps = new EnumMap<Status, List<NameAndError>>(Status.class);
    for (Entry<QName, String> entry : parseResult.getFieldsToSet().entrySet()) {
        QName name = entry.getKey();
        boolean found = false;
        for (PropertySource source : propertySources) {
            PropertyMetaData meta = source.getPropertyMetaData(entry.getKey(), r);
            if (meta != null && !meta.isUnknown()) {
                found = true;
                if (meta.isWritable()) {
                    Object val = parse(name, entry.getValue(), meta.getValueType());
                    try {
                        log.trace("setProperties: name: {} source: {}", name, source.getClass());
                        source.setProperty(name, val, r);
                        knownProps.put(name, new ValueAndType(null, meta.getValueType()));
                        break;
                    } catch (NotAuthorizedException e) {
                        log.warn("setProperties: NotAuthorised to write property: {}", name, e);
                        addErrorProp(errorProps, Response.Status.SC_UNAUTHORIZED, name, "Not authorised");
                        break;
                    } catch (PropertySetException ex) {
                        log.warn("setProperties: PropertySetException when writing property {}", name, ex);
                        addErrorProp(errorProps, ex.getStatus(), name, ex.getErrorNotes());
                        break;
                    }
                } else {
                    log.warn("property is not writable in source: " + source.getClass());
                    addErrorProp(errorProps, Response.Status.SC_FORBIDDEN, name, "Property is read only");
                    break;
                }
            } else {
            // log.debug( "not found in: " + source.getClass().getCanonicalName() );
            }
        }
        if (!found) {
            log.warn("property not found: " + entry.getKey() + " on resource: " + r.getClass());
            addErrorProp(errorProps, Status.SC_NOT_FOUND, entry.getKey(), "Unknown property");
        }
    }
    if (parseResult.getFieldsToRemove() != null) {
        for (QName name : parseResult.getFieldsToRemove()) {
            boolean found = false;
            for (PropertySource source : propertySources) {
                PropertyMetaData meta = source.getPropertyMetaData(name, r);
                if (meta != null && !meta.isUnknown()) {
                    found = true;
                    if (meta.isWritable()) {
                        try {
                            log.trace("clearProperty");
                            source.clearProperty(name, r);
                            knownProps.put(name, new ValueAndType(null, meta.getValueType()));
                            break;
                        } catch (NotAuthorizedException e) {
                            addErrorProp(errorProps, Response.Status.SC_UNAUTHORIZED, name, "Not authorised");
                            break;
                        } catch (PropertySetException ex) {
                            addErrorProp(errorProps, ex.getStatus(), name, ex.getErrorNotes());
                            break;
                        }
                    } else {
                        log.warn("property is not writable in source: " + source.getClass());
                        addErrorProp(errorProps, Response.Status.SC_FORBIDDEN, name, "Property is read only");
                        break;
                    }
                } else {
                // log.debug( "not found in: " + source.getClass().getCanonicalName() );
                }
            }
            if (!found) {
                log.warn("property not found to remove: " + name);
                addErrorProp(errorProps, Status.SC_NOT_FOUND, name, "Unknown property");
            }
        }
    }
    if (log.isDebugEnabled()) {
        if (errorProps.size() > 0) {
            log.debug("errorProps: " + errorProps.size() + " listing property sources:");
            for (PropertySource s : propertySources) {
                log.debug("  source: " + s.getClass().getCanonicalName());
            }
        }
    }
    if (r instanceof CommitableResource) {
        log.trace("resource is commitable, call doCommit");
        CommitableResource cr = (CommitableResource) r;
        cr.doCommit(knownProps, errorProps);
    } else {
        log.trace("resource is not commitable");
    }
    PropFindResponse resp = new PropFindResponse(href, knownProps, errorProps);
    return resp;
}
Also used : Status(io.milton.http.Response.Status) ValueAndType(io.milton.http.values.ValueAndType) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) PropertyMetaData(io.milton.property.PropertySource.PropertyMetaData) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) PropertySource(io.milton.property.PropertySource) NameAndError(io.milton.http.webdav.PropFindResponse.NameAndError) PropertySetException(io.milton.property.PropertySource.PropertySetException) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap)

Example 4 with ValueAndType

use of io.milton.http.values.ValueAndType in project lobcder by skoulouzis.

the class DefaultPropFindPropertyBuilder method processResource.

@Override
public void processResource(List<PropFindResponse> responses, PropFindableResource resource, PropertiesRequest parseResult, String href, int requestedDepth, int currentDepth, String collectionHref) throws NotAuthorizedException, BadRequestException {
    final LinkedHashMap<QName, ValueAndType> knownProperties = new LinkedHashMap<QName, ValueAndType>();
    final ArrayList<NameAndError> unknownProperties = new ArrayList<NameAndError>();
    if (resource instanceof CollectionResource) {
        if (!href.endsWith("/")) {
            href = href + "/";
        }
    }
    Set<QName> requestedFields;
    if (parseResult.isAllProp()) {
        requestedFields = findAllProps(resource);
    } else {
        requestedFields = parseResult.getNames();
    }
    Iterator<QName> it = requestedFields.iterator();
    while (it.hasNext()) {
        QName field = it.next();
        LogUtils.trace(log, "processResoource: find property:", field);
        if (field.getLocalPart().equals("href")) {
            knownProperties.put(field, new ValueAndType(href, String.class));
        } else {
            boolean found = false;
            for (PropertySource source : propertySources) {
                LogUtils.trace(log, "look for field", field, " in property source", source.getClass());
                PropertyMetaData meta = source.getPropertyMetaData(field, resource);
                if (meta != null && !meta.isUnknown()) {
                    Object val;
                    try {
                        val = source.getProperty(field, resource);
                        LogUtils.trace(log, "processResource: got value", val, "from source", source.getClass());
                        if (val == null) {
                            // null, but we still need type information to write it so use meta
                            knownProperties.put(field, new ValueAndType(val, meta.getValueType()));
                        } else {
                            // non-null, so use more robust class info
                            knownProperties.put(field, new ValueAndType(val, val.getClass()));
                        }
                    } catch (NotAuthorizedException ex) {
                        unknownProperties.add(new NameAndError(field, "Not authorised"));
                    }
                    found = true;
                    break;
                }
            }
            if (!found) {
                if (log.isDebugEnabled()) {
                    log.debug("property not found in any property source: " + field.toString());
                }
                unknownProperties.add(new NameAndError(field, null));
            }
        }
    }
    if (log.isDebugEnabled()) {
        if (unknownProperties.size() > 0) {
            log.debug("some properties could not be resolved. Listing property sources:");
            for (PropertySource ps : propertySources) {
                log.debug(" - " + ps.getClass().getCanonicalName());
            }
        }
    }
    // Map<Status, List<NameAndError>> errorProperties = new HashMap<Status, List<NameAndError>>();
    Map<Status, List<NameAndError>> errorProperties = new EnumMap<Status, List<NameAndError>>(Status.class);
    errorProperties.put(Status.SC_NOT_FOUND, unknownProperties);
    PropFindResponse r = new PropFindResponse(href, knownProperties, errorProperties);
    responses.add(r);
    if (requestedDepth > currentDepth && resource instanceof CollectionResource) {
        CollectionResource col = (CollectionResource) resource;
        List<? extends Resource> list = col.getChildren();
        list = new ArrayList<Resource>(list);
        for (Resource child : list) {
            if (child instanceof PropFindableResource) {
                String childName = child.getName();
                if (childName == null) {
                    log.warn("null name for resource of type: " + child.getClass() + " in folder: " + href + " WILL NOT be returned in PROPFIND response!!");
                } else {
                    String childHref = href + Utils.percentEncode(childName);
                    // Note that the new collection href, is just the current href
                    processResource(responses, (PropFindableResource) child, parseResult, childHref, requestedDepth, currentDepth + 1, href);
                }
            }
        }
    }
}
Also used : CollectionResource(io.milton.resource.CollectionResource) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) PropertySource(io.milton.property.PropertySource) PropFindableResource(io.milton.resource.PropFindableResource) Status(io.milton.http.Response.Status) ValueAndType(io.milton.http.values.ValueAndType) QName(javax.xml.namespace.QName) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) PropFindableResource(io.milton.resource.PropFindableResource) PropertyMetaData(io.milton.property.PropertySource.PropertyMetaData) NameAndError(io.milton.http.webdav.PropFindResponse.NameAndError)

Example 5 with ValueAndType

use of io.milton.http.values.ValueAndType in project lobcder by skoulouzis.

the class LdapPropertyMapper method getLdapPropertyValue.

public String getLdapPropertyValue(String prop, Resource resource) throws BadRequestException {
    QName qn = mapToDavProp(prop);
    ValueAndType vt;
    try {
        vt = getProperty(qn, resource);
    } catch (NotAuthorizedException ex) {
        log.trace("property access not authorised");
        vt = null;
    }
    Object propValue;
    if (vt != null && vt.getValue() != null) {
        propValue = vt.getValue();
        return propValue.toString();
    }
    LogUtils.trace(log, "getLdapPropertyValue: property not found: ldap property: ", prop, " - dav prop: ", qn, "resource: ", resource.getClass());
    return null;
}
Also used : ValueAndType(io.milton.http.values.ValueAndType) QName(javax.xml.namespace.QName) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Aggregations

ValueAndType (io.milton.http.values.ValueAndType)6 QName (javax.xml.namespace.QName)5 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)4 Status (io.milton.http.Response.Status)2 NameAndError (io.milton.http.webdav.PropFindResponse.NameAndError)2 PropertySource (io.milton.property.PropertySource)2 PropertyMetaData (io.milton.property.PropertySource.PropertyMetaData)2 XmlWriter (io.milton.http.XmlWriter)1 LdapMappedProp (io.milton.ldap.LdapPropertyMapper.LdapMappedProp)1 PropertySetException (io.milton.property.PropertySource.PropertySetException)1 CollectionResource (io.milton.resource.CollectionResource)1 LdapContact (io.milton.resource.LdapContact)1 PropFindableResource (io.milton.resource.PropFindableResource)1 Resource (io.milton.resource.Resource)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1