<commit_before>package com.ettrema.http.caldav;

import com.bradmcevoy.http.Handler;
import com.bradmcevoy.http.HandlerHelper;
import com.bradmcevoy.http.HttpExtension;
import com.bradmcevoy.http.PropFindableResource;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.ResourceFactory;
import com.bradmcevoy.http.values.CData;
import com.bradmcevoy.http.values.HrefList;
import com.bradmcevoy.http.values.ValueWriters;
import com.bradmcevoy.http.values.WrappedHref;
import com.bradmcevoy.http.webdav.PropFindPropertyBuilder;
import com.bradmcevoy.http.webdav.PropFindXmlGenerator;
import com.bradmcevoy.http.webdav.PropertyMap;
import com.bradmcevoy.http.webdav.PropertyMap.StandardProperty;
import com.bradmcevoy.http.webdav.WebDavProtocol;
import com.bradmcevoy.http.webdav.WebDavResponseHandler;
import com.bradmcevoy.property.PropertySource;
import com.ettrema.http.ICalResource;
import com.ettrema.http.acl.ACLHandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.namespace.QName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author brad
 */
public class CalDavProtocol implements HttpExtension, PropertySource {

    private static final Logger log = LoggerFactory.getLogger( CalDavProtocol.class );

    // Standard caldav properties
    public static final String CALDAV_NS = "urn:ietf:params:xml:ns:caldav";

    // For extension properties
    public static final String CALSERVER_NS = "http://calendarserver.org/ns/";


    private final Set<Handler> handlers;
    private final PropertyMap propertyMapCalDav;
    private final PropertyMap propertyMapCalServer;


    public CalDavProtocol( ResourceFactory resourceFactory, WebDavResponseHandler responseHandler, HandlerHelper handlerHelper, WebDavProtocol webDavProtocol ) {
        propertyMapCalDav = new PropertyMap( CALDAV_NS );
        propertyMapCalDav.add(new CalenderDescriptionProperty());
        propertyMapCalDav.add(new CalendarDataProperty());
        propertyMapCalDav.add(new CalenderHomeSetProperty());
        propertyMapCalDav.add(new CalenderUserAddressSetProperty());
        propertyMapCalDav.add(new ScheduleInboxProperty());
        propertyMapCalDav.add(new ScheduleOutboxProperty());

        propertyMapCalServer = new PropertyMap( CALSERVER_NS );
        propertyMapCalServer.add(new CTagProperty());
        propertyMapCalServer.add(new XMPPProperty());
        propertyMapCalServer.add(new DropBoxProperty());
        propertyMapCalServer.add(new NotificationProperty());

        handlers = new HashSet<Handler>();
        handlers.add( new ACLHandler( responseHandler, handlerHelper ) );
        
        ValueWriters valueWriters = new ValueWriters();
        PropFindXmlGenerator gen = new PropFindXmlGenerator( valueWriters );
        webDavProtocol.addPropertySource( this );
        PropFindPropertyBuilder propertyBuilder = new PropFindPropertyBuilder( webDavProtocol.getPropertySources() );

        //Adding supported reports
        webDavProtocol.addReport(new MultiGetReport(resourceFactory, propertyBuilder, gen ));
        webDavProtocol.addReport(new ACLPrincipalPropSetReport());
        webDavProtocol.addReport(new PrincipalMatchReport());
        webDavProtocol.addReport(new PrincipalPropertySearchReport());
        webDavProtocol.addReport(new ExpandPropertyReport());       
    }

    public Set<Handler> getHandlers() {
        return Collections.unmodifiableSet( handlers );
    }



    //TODO: remove debug logging once it's working
    public Object getProperty( QName name, Resource r ) {
        log.debug( "getProperty: " + name.getLocalPart() );
        Object o;
        if( propertyMapCalDav.hasProperty( name )) {
            o = propertyMapCalDav.getProperty( name, r );
        } else {
            o = propertyMapCalServer.getProperty( name, r );
        }
        log.debug( "result : "+o );
        return o;
    }

    public void setProperty( QName name, Object value, Resource r ) {
        log.debug( "setProperty: " + name.getLocalPart() );
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public PropertyMetaData getPropertyMetaData( QName name, Resource r ) {
        log.debug( "getPropertyMetaData: " + name.getLocalPart() );
        if( propertyMapCalDav.hasProperty( name )) {
            return propertyMapCalDav.getPropertyMetaData( name, r );
        } else {
            return propertyMapCalServer.getPropertyMetaData( name, r );
        }
    }

    public void clearProperty( QName name, Resource r ) {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public List<QName> getAllPropertyNames( Resource r ) {
        log.debug( "getAllPropertyNames" );
        List<QName> list = new ArrayList<QName>();
        list.addAll( propertyMapCalDav.getAllPropertyNames( r ) );
        list.addAll( propertyMapCalServer.getAllPropertyNames( r ) );
        return list;
    }

    class CalendarDataProperty implements StandardProperty<CData> {
        public String fieldName() {
            return "calendar-data";
        }

        public CData getValue( PropFindableResource res ) {
            log.debug( "getValue: " + res.getClass());
            if( res instanceof ICalResource) {
                ICalResource ical = (ICalResource) res;
                return new CData( ical.getICalData() );
            } else {
                log.warn( "getValue: not a ICalResource");
                return null;
            }
        }

        public Class<CData> getValueClass() {
            return CData.class;<commit_msg>improved logging<commit_after>package com.ettrema.http.caldav;

import com.bradmcevoy.http.Handler;
import com.bradmcevoy.http.HandlerHelper;
import com.bradmcevoy.http.HttpExtension;
import com.bradmcevoy.http.PropFindableResource;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.ResourceFactory;
import com.bradmcevoy.http.values.CData;
import com.bradmcevoy.http.values.HrefList;
import com.bradmcevoy.http.values.ValueWriters;
import com.bradmcevoy.http.values.WrappedHref;
import com.bradmcevoy.http.webdav.PropFindPropertyBuilder;
import com.bradmcevoy.http.webdav.PropFindXmlGenerator;
import com.bradmcevoy.http.webdav.PropertyMap;
import com.bradmcevoy.http.webdav.PropertyMap.StandardProperty;
import com.bradmcevoy.http.webdav.WebDavProtocol;
import com.bradmcevoy.http.webdav.WebDavResponseHandler;
import com.bradmcevoy.property.PropertySource;
import com.ettrema.http.ICalResource;
import com.ettrema.http.acl.ACLHandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.namespace.QName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author brad
 */
public class CalDavProtocol implements HttpExtension, PropertySource {

    private static final Logger log = LoggerFactory.getLogger( CalDavProtocol.class );

    // Standard caldav properties
    public static final String CALDAV_NS = "urn:ietf:params:xml:ns:caldav";

    // For extension properties
    public static final String CALSERVER_NS = "http://calendarserver.org/ns/";


    private final Set<Handler> handlers;
    private final PropertyMap propertyMapCalDav;
    private final PropertyMap propertyMapCalServer;


    public CalDavProtocol( ResourceFactory resourceFactory, WebDavResponseHandler responseHandler, HandlerHelper handlerHelper, WebDavProtocol webDavProtocol ) {
        propertyMapCalDav = new PropertyMap( CALDAV_NS );
        propertyMapCalDav.add(new CalenderDescriptionProperty());
        propertyMapCalDav.add(new CalendarDataProperty());
        propertyMapCalDav.add(new CalenderHomeSetProperty());
        propertyMapCalDav.add(new CalenderUserAddressSetProperty());
        propertyMapCalDav.add(new ScheduleInboxProperty());
        propertyMapCalDav.add(new ScheduleOutboxProperty());

        propertyMapCalServer = new PropertyMap( CALSERVER_NS );
        propertyMapCalServer.add(new CTagProperty());
        propertyMapCalServer.add(new XMPPProperty());
        propertyMapCalServer.add(new DropBoxProperty());
        propertyMapCalServer.add(new NotificationProperty());

        handlers = new HashSet<Handler>();
        handlers.add( new ACLHandler( responseHandler, handlerHelper ) );
        
        ValueWriters valueWriters = new ValueWriters();
        PropFindXmlGenerator gen = new PropFindXmlGenerator( valueWriters );
        webDavProtocol.addPropertySource( this );
        PropFindPropertyBuilder propertyBuilder = new PropFindPropertyBuilder( webDavProtocol.getPropertySources() );

        //Adding supported reports
        webDavProtocol.addReport(new MultiGetReport(resourceFactory, propertyBuilder, gen ));
        webDavProtocol.addReport(new ACLPrincipalPropSetReport());
        webDavProtocol.addReport(new PrincipalMatchReport());
        webDavProtocol.addReport(new PrincipalPropertySearchReport());
        webDavProtocol.addReport(new ExpandPropertyReport());       
    }

    public Set<Handler> getHandlers() {
        return Collections.unmodifiableSet( handlers );
    }



    //TODO: remove debug logging once it's working
    public Object getProperty( QName name, Resource r ) {
        log.trace( "getProperty: {}", name.getLocalPart() );
        Object o;
        if( propertyMapCalDav.hasProperty( name )) {
            o = propertyMapCalDav.getProperty( name, r );
        } else {
            o = propertyMapCalServer.getProperty( name, r );
        }
        log.debug( "result : "+o );
        return o;
    }

    public void setProperty( QName name, Object value, Resource r ) {
        log.trace( "setProperty: {}", name.getLocalPart() );
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public PropertyMetaData getPropertyMetaData( QName name, Resource r ) {
        log.trace( "getPropertyMetaData: {}", name.getLocalPart() );
        if( propertyMapCalDav.hasProperty( name )) {
            return propertyMapCalDav.getPropertyMetaData( name, r );
        } else {
            return propertyMapCalServer.getPropertyMetaData( name, r );
        }
    }

    public void clearProperty( QName name, Resource r ) {
        throw new UnsupportedOperationException( "Not supported yet." );
    }

    public List<QName> getAllPropertyNames( Resource r ) {
        log.trace( "getAllPropertyNames" );
        List<QName> list = new ArrayList<QName>();
        list.addAll( propertyMapCalDav.getAllPropertyNames( r ) );
        list.addAll( propertyMapCalServer.getAllPropertyNames( r ) );
        return list;
    }

    class CalendarDataProperty implements StandardProperty<CData> {
        public String fieldName() {
            return "calendar-data";
        }

        public CData getValue( PropFindableResource res ) {
            log.trace( "getValue: " + res.getClass());
            if( res instanceof ICalResource) {
                ICalResource ical = (ICalResource) res;
                return new CData( ical.getICalData() );
            } else {
                log.warn( "getValue: not a ICalResource");
                return null;
            }
        }

        public Class<CData> getValueClass() {
            return CData.class;
{'fraction_non_alphanumeric': 0.09334272631208172, 'fraction_numerical': 0.0003522367030644593, 'mean_word_length': 3.390027058368767, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3229630', 'n_tokens_mistral': 3285, 'n_tokens_neox': 3131, 'n_words': 668}