<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.uva.cs.lobcder.webDav.resources;

import com.bradmcevoy.http.Auth;
import com.bradmcevoy.http.PropFindableResource;
import com.bradmcevoy.http.Request;
import com.bradmcevoy.http.Request.Method;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import com.bradmcevoy.http.values.HrefList;
import com.bradmcevoy.http.webdav.PropertyMap;
import com.bradmcevoy.property.MultiNamespaceCustomPropertyResource;
import com.bradmcevoy.property.PropertySource.PropertyAccessibility;
import com.bradmcevoy.property.PropertySource.PropertyMetaData;
import com.bradmcevoy.property.PropertySource.PropertySetException;
import com.ettrema.http.AccessControlledResource;
import com.ettrema.http.acl.DavPrincipal;
import com.ettrema.http.acl.DavPrincipals.AbstractDavPrincipal;
import com.ettrema.http.acl.Principal;
import java.io.IOException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.xml.namespace.QName;
import nl.uva.cs.lobcder.auth.AuthI;
import nl.uva.cs.lobcder.auth.MyPrincipal;
import nl.uva.cs.lobcder.auth.Permissions;
import nl.uva.cs.lobcder.catalogue.CatalogueException;
import nl.uva.cs.lobcder.catalogue.JDBCatalogue;
import nl.uva.cs.lobcder.frontend.WebDavServlet;
import nl.uva.cs.lobcder.resources.LogicalData;
import nl.uva.cs.lobcder.resources.MyStorageSite;
import nl.uva.cs.lobcder.resources.PDRI;
import nl.uva.cs.lobcder.resources.PDRIFactory;
import nl.uva.cs.lobcder.util.Constants;

/**
 *
 * @author <NAME>
 */
public class WebDataResource implements PropFindableResource, Resource, AccessControlledResource, MultiNamespaceCustomPropertyResource {

    private LogicalData logicalData;
    private final JDBCatalogue catalogue;
    private static final boolean debug = true;
    private final Map<QName, Object> customProperties = new HashMap<QName, Object>();
    private final Map<String, PropertyMap.StandardProperty> userPrivledges = new HashMap<String, PropertyMap.StandardProperty>();
    //Collection<Integer> roles = null;
    //private String uname;
    private static AuthI auth;

    static {
        try {
            String jndiName = "bean/auth";
            javax.naming.Context ctx = new InitialContext();
            if (ctx == null) {
                throw new Exception("JNDI could not create InitalContext ");
            }
            javax.naming.Context envContext = (javax.naming.Context) ctx.lookup("java:/comp/env");
            auth = (AuthI) envContext.lookup(jndiName);
        } catch (Exception ex) {
            Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public WebDataResource(JDBCatalogue catalogue, LogicalData logicalData) {
        this.logicalData = logicalData;
//        if (!logicalData.getType().equals(Constants.LOGICAL_DATA)) {
//            throw new Exception("The logical data has the wonrg type: " + logicalData.getType());
//        }
        this.catalogue = catalogue;

        //We can't init the data dist props here cause there is no authorization 
        //yet. So the getPrincipal will return null
//        initProps();

    }

    private void initProps() {
        if (customProperties.isEmpty()) {
            customProperties.put(Constants.DRI_SUPERVISED_PROP_NAME, String.valueOf(getLogicalData().getSupervised()));
            customProperties.put(Constants.DRI_CHECKSUM_PROP_NAME, String.valueOf(getLogicalData().getChecksum()));
            customProperties.put(Constants.DRI_LAST_VALIDATION_DATE_PROP_NAME, String.valueOf(getLogicalData().getLastValidationDate()));
            customProperties.put(Constants.DATA_DIST_PROP_NAME, null);

//            getLogicalData().getPermissions()
//            customProperties.put(Constants.DAV_ACL_PROP_NAME, null);
//            List<Priviledge> list = getPriviledges(null);
//            customProperties.put(Constants.DAV_CURRENT_USER_PRIVILAGE_SET_PROP_NAME, list);
//        if(getLogicalData().getSupervised() != null) {
//            DataDistProperty ddip = new DataDistProperty();
//            ddip.setFormattedValue(getLogicalData().getSupervised().toString());
//            customProperties.put(Constants.DATA_DIST_PROP_NAME, ddip);
//        }
        }
    }

    @Override
    public Date getCreateDate() {
//        debug("getCreateDate.");
        return new Date(getLogicalData().getCreateDate());
    }

    @Override
    public String getUniqueId() {
//        debug("getUniqueId.");
        return String.valueOf(getLogicalData().getUID());
    }

    @Override
    public String getName() {
//        debug("getName.");
        return getLogicalData().getLDRI().getName();
    }

    @Override
    public Object authenticate(String user, String password) {
        debug("authenticate.\n"
                + "\t user: " + user
                + "\t password: " + password);
        String token = password;
        MyPrincipal principal = auth.checkToken(token);
        WebDavServlet.request().setAttribute("vph-user", principal);
        debug("getUserId: "+principal.getUserId());
        debug("getRolesStr: "+principal.getRolesStr());
        return principal;
    }

    @Override
    public boolean authorise(Request request, Method method, Auth auth) {
//        System.err.println(WebDavServlet.request().getUserPrincipal());
        //Object permission = getPermissionForTheLogicalData();
        boolean authorized = true;
        if (authorized) {
            String absPath = null;
            String absURL = null;
            String acceptHeader = null;
            String fromAddress = null;
            String remoteAddr = null;
            String cnonce = null;
            String nc = null;
            String nonce = null;
            String password = null;
            String qop = null;
            String relm = null;
            String responseDigest = null;
            String uri = null;
            String user = null;
            Object tag = null;
            if (request != null) {<commit_msg>Fixed 500 bug. For wrong credentials it should return 401 but due to null principal it was returning 500<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.uva.cs.lobcder.webDav.resources;

import com.bradmcevoy.http.Auth;
import com.bradmcevoy.http.PropFindableResource;
import com.bradmcevoy.http.Request;
import com.bradmcevoy.http.Request.Method;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import com.bradmcevoy.http.values.HrefList;
import com.bradmcevoy.http.webdav.PropertyMap;
import com.bradmcevoy.property.MultiNamespaceCustomPropertyResource;
import com.bradmcevoy.property.PropertySource.PropertyAccessibility;
import com.bradmcevoy.property.PropertySource.PropertyMetaData;
import com.bradmcevoy.property.PropertySource.PropertySetException;
import com.ettrema.http.AccessControlledResource;
import com.ettrema.http.acl.DavPrincipal;
import com.ettrema.http.acl.DavPrincipals.AbstractDavPrincipal;
import com.ettrema.http.acl.Principal;
import java.io.IOException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.xml.namespace.QName;
import nl.uva.cs.lobcder.auth.AuthI;
import nl.uva.cs.lobcder.auth.MyPrincipal;
import nl.uva.cs.lobcder.auth.Permissions;
import nl.uva.cs.lobcder.catalogue.CatalogueException;
import nl.uva.cs.lobcder.catalogue.JDBCatalogue;
import nl.uva.cs.lobcder.frontend.WebDavServlet;
import nl.uva.cs.lobcder.resources.LogicalData;
import nl.uva.cs.lobcder.resources.MyStorageSite;
import nl.uva.cs.lobcder.resources.PDRI;
import nl.uva.cs.lobcder.resources.PDRIFactory;
import nl.uva.cs.lobcder.util.Constants;

/**
 *
 * @author <NAME>
 */
public class WebDataResource implements PropFindableResource, Resource, AccessControlledResource, MultiNamespaceCustomPropertyResource {

    private LogicalData logicalData;
    private final JDBCatalogue catalogue;
    private static final boolean debug = true;
    private final Map<QName, Object> customProperties = new HashMap<QName, Object>();
    private final Map<String, PropertyMap.StandardProperty> userPrivledges = new HashMap<String, PropertyMap.StandardProperty>();
    //Collection<Integer> roles = null;
    //private String uname;
    private static AuthI auth;

    static {
        try {
            String jndiName = "bean/auth";
            javax.naming.Context ctx = new InitialContext();
            if (ctx == null) {
                throw new Exception("JNDI could not create InitalContext ");
            }
            javax.naming.Context envContext = (javax.naming.Context) ctx.lookup("java:/comp/env");
            auth = (AuthI) envContext.lookup(jndiName);
        } catch (Exception ex) {
            Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public WebDataResource(JDBCatalogue catalogue, LogicalData logicalData) {
        this.logicalData = logicalData;
//        if (!logicalData.getType().equals(Constants.LOGICAL_DATA)) {
//            throw new Exception("The logical data has the wonrg type: " + logicalData.getType());
//        }
        this.catalogue = catalogue;

        //We can't init the data dist props here cause there is no authorization 
        //yet. So the getPrincipal will return null
//        initProps();

    }

    private void initProps() {
        if (customProperties.isEmpty()) {
            customProperties.put(Constants.DRI_SUPERVISED_PROP_NAME, String.valueOf(getLogicalData().getSupervised()));
            customProperties.put(Constants.DRI_CHECKSUM_PROP_NAME, String.valueOf(getLogicalData().getChecksum()));
            customProperties.put(Constants.DRI_LAST_VALIDATION_DATE_PROP_NAME, String.valueOf(getLogicalData().getLastValidationDate()));
            customProperties.put(Constants.DATA_DIST_PROP_NAME, null);

//            getLogicalData().getPermissions()
//            customProperties.put(Constants.DAV_ACL_PROP_NAME, null);
//            List<Priviledge> list = getPriviledges(null);
//            customProperties.put(Constants.DAV_CURRENT_USER_PRIVILAGE_SET_PROP_NAME, list);
//        if(getLogicalData().getSupervised() != null) {
//            DataDistProperty ddip = new DataDistProperty();
//            ddip.setFormattedValue(getLogicalData().getSupervised().toString());
//            customProperties.put(Constants.DATA_DIST_PROP_NAME, ddip);
//        }
        }
    }

    @Override
    public Date getCreateDate() {
//        debug("getCreateDate.");
        return new Date(getLogicalData().getCreateDate());
    }

    @Override
    public String getUniqueId() {
//        debug("getUniqueId.");
        return String.valueOf(getLogicalData().getUID());
    }

    @Override
    public String getName() {
//        debug("getName.");
        return getLogicalData().getLDRI().getName();
    }

    @Override
    public Object authenticate(String user, String password) {
        debug("authenticate.\n"
                + "\t user: " + user
                + "\t password: " + password);
        String token = password;
        MyPrincipal principal = auth.checkToken(token);
        if (principal != null) {
            WebDavServlet.request().setAttribute("vph-user", principal);
            debug("getUserId: " + principal.getUserId());
            debug("getRolesStr: " + principal.getRolesStr());
        }
        return principal;
    }

    @Override
    public boolean authorise(Request request, Method method, Auth auth) {
//        System.err.println(WebDavServlet.request().getUserPrincipal());
        //Object permission = getPermissionForTheLogicalData();
        boolean authorized = true;
        if (authorized) {
            String absPath = null;
            String absURL = null;
            String acceptHeader = null;
            String fromAddress = null;
            String remoteAddr = null;
            String cnonce = null;
            String nc = null;
            String nonce = null;
            String password = <PASSWORD>;
            String qop = null;
            String relm = null;
            String responseDigest = null;
            String uri = null;
            String user = null;
            Object tag = null;
            if (request != null) {
{'fraction_non_alphanumeric': 0.10815268614514609, 'fraction_numerical': 0.000706880301602262, 'mean_word_length': 3.0345373891001266, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3405723', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3664, 'n_words': 737}