Subversion Repositories bacoAlunos

Rev

Rev 1814 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.domain;

import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import pt.estgp.estgweb.domain.typesgenservices.repositorydocuments.RepositoryFileProxy;
import pt.estgp.estgweb.services.virtualfilesystem.PageContentAccessPolicyAcl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by jorgemachado on 03/02/16.
 */

public abstract class PageContentImpl extends PageContent implements ClearanceRuled
{

    private static 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(PageContentImpl.class);

    private UserSession sess = null;
    private Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation,Boolean> userSessionClearences = null;
    private boolean childsInitialized = false;
    private boolean parentInitialized = false;


    static Genson genson;

    static Genson gensonSerialize;


    static {
        gensonSerialize = new GensonBuilder()
                .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
                .useMethods(true)
                .useFields(false)
                .useClassMetadata(true)
                .useRuntimeType(true)
                //.include("visible")
                .include(1.5.0/docs/api/java/lang/Boolean.html">Boolean.class)
                .exclude("clear")
                .exclude("clearanceInitialized")
                .include(1.5.0/docs/api/java/lang/Integer.html">Integer.class)
                .include(1.5.0/docs/api/java/lang/Long.html">Long.class)
                .include(1.5.0/docs/api/java/lang/Double.html">Double.class)
                .include(1.5.0/docs/api/java/lang/Float.html">Float.class)
                        //.useFields(false)  //COM ISTO A SLUG VAI A NULL E SEM ISTO DÁ NULLPOINTER EXCEPTION. ISTO NO SERVIÇO!!!
                .include(PageContentImpl.class)
                .include(PageSectionImpl.class)
                .include(1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation.class)
                .include("title", PageContent.class)
                        //.include("directory",PageContent.class)
                        //.include("file", PageContent.class)
                .include("description",PageContent.class)
                .include("path",PageContent.class)
                .include("acl",PageContent.class)
                .include("slug",PageContent.class)
                .include("pathForIdHtml",PageContent.class)
                .include("html", PageText.class)
                .include("repositoryId",PageRepositoryFile.class)
                        //.include("url",PageContent.class) PARA DAR URL TRUE TIVE QUE COMENTAR?? ANTES DE COMENTAR DAVA NULL
                        //.include("textBox", PageContent.class)
                        //.include("getAclJson", PageContentImpl.class)
                .include("clearances", PageContentImpl.class)//como FUNCIONA PARA PÔR AS ACL'S DIREITINHAS??
                .include("clearences", PageContentImpl.class)
                .include("userSessionClearences", PageContentImpl.class)
                        //.include("pageContent", PageContentImpl.class)
                        //.include("isClear", PageContentImpl.class)
                        //.include("isDirectory", PageContentImpl.class)
                .include("topLayoutSection", PageSection.class)
                //.include("UserSessionClearences", PageContentImpl.class)
                .exclude("childsInitialized", PageContentImpl.class)
                .exclude("parentInitialized", PageContentImpl.class)

                .exclude("parent", PageContent.class)


                .include("repositoryFileProxy",PageRepositoryFile.class)
                .include("repositoryFile",RepositoryFileProxy.class)
                .include(RepositoryFileProxy.class)
                .include(RepositoryFileImpl.class)
                .include(RepositoryFileVersionImpl.class)

                .include("name", RepositoryFileVersion.class)
                .include("extension", RepositoryFileVersion.class)
                .include("contentType", RepositoryFileVersion.class)
                .include("lastVersion", RepositoryFile.class)
                .include("size", RepositoryFileVersion.class)
                .include("repositoryFileProxy", RepositoryDocumentFileImpl.class)
                .exclude("repositoryFile", RepositoryFileVersion.class)
                .exclude("publishDate", RepositoryDocumentImpl.class)
                .exclude("closeDate", RepositoryDocumentImpl.class)
                        //O sistema nao constroi o RepositoryFile a partir da classe
                        //mas como guarda o stream quando serializar aparece outra vez o ficheiro
                .include("repositoryFile", RepositoryFileProxy.class) //DIFERENCA COM O ANTERIOR
                .exclude("repositoryFileVersions",RepositoryFile.class)
                .exclude("sourceModule",RepositoryFile.class)


                .create();

    }

    static {
        GensonBuilder gensonBuilder = new GensonBuilder()
                .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
                .useMethods(true)
                .useFields(false)
                .useClassMetadata(true)
                .useRuntimeType(true)

                .exclude("clear")
                .exclude("clearanceInitialized")
                .include(1.5.0/docs/api/java/lang/Integer.html">Integer.class)
                .include(1.5.0/docs/api/java/lang/Long.html">Long.class)
                .include(1.5.0/docs/api/java/lang/Double.html">Double.class)
                .include(1.5.0/docs/api/java/lang/Float.html">Float.class)
                //.useFields(false)  //COM ISTO A SLUG VAI A NULL E SEM ISTO DÁ NULLPOINTER EXCEPTION. ISTO NO SERVIÇO!!!
                .include(PageContentImpl.class)
                .include(PageSectionImpl.class)
                .include(1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation.class)
                .include("title", PageContent.class)
                //.include("directory",PageContent.class)
                //.include("file", PageContent.class)
                .include("visible", PageContent.class) //TIVE QUE INCLUIR PORQUE PRECISO DO MÉTODO ISVISIBLE
                .include("description", PageContent.class)
                .include("path", PageContent.class)
                .include("acl",PageContent.class)
                .include("slug",PageContent.class)
                .include("pathForIdHtml", PageContent.class)
                .include("html", PageText.class)
                .include("repositoryId", PageRepositoryFile.class)
                //.include("url",PageContent.class) PARA DAR URL TRUE TIVE QUE COMENTAR?? ANTES DE COMENTAR DAVA NULL
                //.include("textBox", PageContent.class)
                //.include("getAclJson", PageContentImpl.class)
                .include("clearances", PageContentImpl.class)//como FUNCIONA PARA PÔR AS ACL'S DIREITINHAS??
                .include("clearences", PageContentImpl.class)
                .include("userSessionClearences", PageContentImpl.class)
                //.include("UserSessionClearences", PageContentImpl.class)
                //.include("pageContent", PageContentImpl.class)
                //.include("isClear", PageContentImpl.class)
                //.include("isDirectory", PageContentImpl.class)
                .exclude("childsInitialized", PageContentImpl.class)
                .exclude("parentInitialized", PageContentImpl.class)
                .exclude("parent", PageContent.class);




        genson = gensonBuilder.create();
    }


    public boolean isDirectory()
    {
        return this instanceof PageSection;
    }

    public boolean isFile()
    {
        return this instanceof PageRepositoryFile;
    }

    public boolean isUrl()
    {
        return this instanceof PageExternalUrl;
    }

    public boolean isTextBox()
    {
        return this instanceof PageText;
    }

    public static PageContentImpl loadFromJson(1.5.0/docs/api/java/lang/String.html">String json) throws 1.5.0/docs/api/java/io/IOException.html">IOException{
        return genson.deserialize(json,PageContentImpl.class);
    }

    public static PageExternalUrlImpl loadFromJsonUrl(1.5.0/docs/api/java/lang/String.html">String json) throws 1.5.0/docs/api/java/io/IOException.html">IOException{
        return genson.deserialize(json,PageExternalUrlImpl.class);
    }

    public static PageRepositoryFileImpl loadFromJsonFile(1.5.0/docs/api/java/lang/String.html">String json) throws 1.5.0/docs/api/java/io/IOException.html">IOException{
        return genson.deserialize(json,PageRepositoryFileImpl.class);
    }

    public static PageTextImpl loadFromJsonText(1.5.0/docs/api/java/lang/String.html">String json) throws 1.5.0/docs/api/java/io/IOException.html">IOException{
        return genson.deserialize(json,PageTextImpl.class);
    }

    public 1.5.0/docs/api/java/lang/String.html">String toJson()
    {
        return gensonSerialize.serialize(this);
    }

    public JSONObject toJsonObject() throws JSONException{
        return new JSONObject(toJson());
    }

    public static JSONArray toJsonArray(List<PageContentImpl> itemGroup) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
        return new JSONArray(gensonSerialize.serialize(itemGroup));
    }

    public static class 1.5.0/docs/api/java/security/acl/Acl.html">Acl
    {
        public static 1.5.0/docs/api/java/security/acl/Acl.html">Acl AUTHENTICATED_ACL = new 1.5.0/docs/api/java/security/acl/Acl.html">Acl(1.5.0/docs/api/java/security/acl/Acl.html">Acl.Access.PERMIT.name(),
                1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/lang/reflect/Type.html">Type.AUTHENTICATED.name(),"",1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation.READ_CONTENTS.name());
        public static ArrayList<Acl> AUTHENTICATED_ACL_SIMPLE_LIST = new ArrayList<Acl>();
        static{
            AUTHENTICATED_ACL_SIMPLE_LIST.add(AUTHENTICATED_ACL);
        }

        public 1.5.0/docs/api/java/security/acl/Acl.html">Acl(1.5.0/docs/api/java/lang/String.html">String access, 1.5.0/docs/api/java/lang/String.html">String type, 1.5.0/docs/api/java/lang/String.html">String id, 1.5.0/docs/api/java/lang/String.html">String operation) {
            this.access = access;
            this.type = type;
            this.id = id;
            this.operation = operation;
        }

        public 1.5.0/docs/api/java/security/acl/Acl.html">Acl() {
        }

        public static enum Access
        {
            PERMIT,
            DENY //default
        }
        public static enum 1.5.0/docs/api/java/lang/reflect/Type.html">Type
        {
            ALL,
            ANONYMOUS, //ONLY SUPER USER SHOULD ADD ANONYMOUS PERMISSIONS
            AUTHENTICATED,
            ROLE,
            USER,
            COURSEUNIT //SPECIFIC PERMISSIONS RELATIVE TO COURSE UNITS
        }
        public static enum 1.5.0/docs/api/java/rmi/server/Operation.html">Operation
        {
            ALL, //SPECIAL OPERATION FOR DENY AND PERMIT REPLACE FULL CONTROL

            READ_CONTENTS,

            READ_VERSIONS,
            READ_INVISIBLES,


            ORDER_CONTENTS,

            ADD_SECTIONS, //ADD TEXT USA ESTA PERMISSAO
            ADD_FILES,
            ADD_URLS,
            ADD_MODULES,

            REMOVE_SECTIONS,
            REMOVE_FILES,
            REMOVE_URLS,
            REMOVE_MODULES,

            EDIT_SECTIONS,
            EDIT_FILES,
            EDIT_URLS,
            EDIT_MODULES;

            public static 1.5.0/docs/api/java/rmi/server/Operation.html">Operation parse(1.5.0/docs/api/java/lang/String.html">String clearance)
            {
                if(clearance == null)
                    return null;
                for(1.5.0/docs/api/java/rmi/server/Operation.html">Operation op: 1.5.0/docs/api/java/rmi/server/Operation.html">Operation.values())
                {
                    if(op.name().equals(clearance))
                        return op;
                }
                return null;
            }


        }

        public static enum CourseUnitsId
        {
            COURSE_UNIT_TEACHER("page.content.course.unit.teacher"),
            COURSE_UNIT_STUDENT("page.content.course.unit.student"),
            COURSE_UNIT_RESP_TEACHER("page.content.course.unit.resp.teacher"),
            COURSE_UNIT_COURSE_COMISSION("page.content.course.unit.course.comission");

            1.5.0/docs/api/java/lang/String.html">String msgKey;


            CourseUnitsId(1.5.0/docs/api/java/lang/String.html">String msgKey) {
                this.msgKey = msgKey;
            }
        }

        private 1.5.0/docs/api/java/lang/String.html">String access;
        private 1.5.0/docs/api/java/lang/String.html">String type;
        private 1.5.0/docs/api/java/lang/String.html">String id;
        private 1.5.0/docs/api/java/lang/String.html">String operation;

        public 1.5.0/docs/api/java/lang/String.html">String getAccess() {
            return access;
        }

        public void setAccess(1.5.0/docs/api/java/lang/String.html">String access) {
            this.access = access;
        }

        public void setAccessEnum(Access access) {
            this.access = access.name();
        }

        public 1.5.0/docs/api/java/lang/String.html">String getType() {
            return type;
        }

        public void setType(1.5.0/docs/api/java/lang/String.html">String type) {
            this.type = type;
        }
        public void setTypeEnum(1.5.0/docs/api/java/lang/reflect/Type.html">Type type) {
            this.type = type.name();
        }

        public 1.5.0/docs/api/java/lang/String.html">String getId() {
            return id;
        }

        public void setId(1.5.0/docs/api/java/lang/String.html">String id) {
            this.id = id;
        }

        public 1.5.0/docs/api/java/lang/String.html">String getOperation() {
            return operation;
        }

        public void setOperation(1.5.0/docs/api/java/lang/String.html">String operation) {
            this.operation = operation;
        }

        public void setOperationEnum(1.5.0/docs/api/java/rmi/server/Operation.html">Operation operation) {
            this.operation = operation.name();
        }

        public void setCourseUnitIdEnum(CourseUnitsId courseUnitIdEnum) {
            this.id = courseUnitIdEnum.name();
        }

        public Access getAccessEnum()
        {
            for(Access a : Access.values())
            {
                if(a.name().equals(getAccess()))
                    return a;
            }
            return Access.DENY;
        }

        public 1.5.0/docs/api/java/lang/reflect/Type.html">Type getTypeEnum()
        {
            for(1.5.0/docs/api/java/lang/reflect/Type.html">Type a : 1.5.0/docs/api/java/lang/reflect/Type.html">Type.values())
            {
                if(a.name().equals(getType()))
                    return a;
            }
            return 1.5.0/docs/api/java/lang/reflect/Type.html">Type.ROLE;
        }

        public 1.5.0/docs/api/java/rmi/server/Operation.html">Operation getOperationEnum()
        {
            for(1.5.0/docs/api/java/rmi/server/Operation.html">Operation a : 1.5.0/docs/api/java/rmi/server/Operation.html">Operation.values())
            {
                if(a.name().equals(getOperation()))
                    return a;
            }
            return null;
        }

        public CourseUnitsId getCourseUnitIdEnum()
        {
            for(CourseUnitsId a : CourseUnitsId.values())
            {
                if(a.name().equals(getId()))
                    return a;
            }
            return null;
        }
    }

    /**
     * Empty Array Means no ACL's defined
     * build a json array form ACL's
     * @return example [{"access":"PERMIT","operation":"FULL_CONTROL","id":"services","type":"ROLE"}]
     * @throws JSONException
     */

    public JSONArray getAclJson() throws JSONException
    {
        if(getAcl() == null || getAcl().trim().length() == 0)
            return new JSONArray();
        return new JSONArray(getAcl());
    }

    /**
     * Sets the string ACL with the proper json
     * @param json
     * @throws JSONException
     */

    public void setAclJson(JSONArray json) throws JSONException
    {
        if(json == null)
            setAcl(null);
        else
            setAcl(json.toString());
    }

    /**
     * sets the string Acl with the json built from given List
     * @param acls
     * @throws JSONException
     */

    public void setAclList(ArrayList<Acl> acls) throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException {
        if(acls == null || acls.size() == 0)
        {
            setAcl(null);
            return;
        }
        setAcl(getAclJson(acls).toString());
    }

    /**
     * Empty Array Means no ACL's defined allays
     * @return allays a list if no acls return empty list
     * @throws JSONException
     */

    public ArrayList<Acl> getAclList() throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException {
        ArrayList<Acl> acls = new ArrayList<Acl>();
        if(getAcl() == null || getAcl().trim().length() == 0)
            return acls;
        Genson genson = getGenson();
        ArrayList<HashMap<String,String>> aclsDes = genson.deserialize(getAcl(),1.5.0/docs/api/java/util/ArrayList.html">ArrayList.class);
        for(HashMap<String,String> acl: aclsDes)
        {
            1.5.0/docs/api/java/security/acl/Acl.html">Acl a = new 1.5.0/docs/api/java/security/acl/Acl.html">Acl();
            a.setType(acl.get("type"));
            a.setOperation(acl.get("operation"));
            a.setAccess(acl.get("access"));
            a.setId(acl.get("id"));
            acls.add(a);
        }
        return acls;
    }

    /**
     * Add a acl to String json at the end
     * @param acl
     * @throws IOException
     * @throws JSONException
     */

    public void addAcl(1.5.0/docs/api/java/security/acl/Acl.html">Acl acl) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
        if(acl == null)
            return;
        ArrayList<Acl> acls = getAclList();
        acls.add(acl);
        setAclList(acls);
    }

    private static Genson getGenson(){
        Genson genson = new GensonBuilder()
                .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
                .include("access", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
                .include("type", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
                .include("id", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
                .include("operation", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
                .create();
        return genson;
    }

    /**
     * Utilitário
     * @param aclList
     * @return
     * @throws IOException
     * @throws JSONException
     */

    public static JSONArray getAclJson(ArrayList<Acl> aclList) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
        if(aclList == null)
            return new JSONArray();
        Genson genson = getGenson();
        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(aclList);
        //System.out.println(json);
        return new JSONArray(json);
    }


    public boolean isChildsInitialized() {
        return childsInitialized;
    }

    public void setChildsInitialized(boolean childsInitialized) {
        this.childsInitialized = childsInitialized;
    }

    public boolean isParentInitialized() {
        return parentInitialized;
    }

    public void setParentInitialized(boolean parentInitialized) {
        this.parentInitialized = parentInitialized;
    }

    /**
     * Metodo de testes
     * @param args
     * @throws IOException
     * @throws JSONException
     */

    public static void main(1.5.0/docs/api/java/lang/String.html">String [] args) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {



    }

    public boolean canAdmin(UserSessionImpl u)
    {
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
        {
            logger.warn("Clearances checked for Mr:" + sess.getCookie() + " but clearing for Mr:" + u.getCookie());
            return false;
        }
        else if(userSessionClearences != null)
        {
            for(1.5.0/docs/api/java/util/Map.html">Map.Entry<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation,Boolean> entry: userSessionClearences.entrySet())
            {
                if(entry.getValue() == true &&
                        !entry.getKey().name().startsWith("READ"))
                {
                    return true;
                }
            }
            return false;
        }
        else
        {
            logger.warn("Checking clearance without sessionClearances initialized, not allowed");
            return false;
        }
    }




    public boolean isClear(UserSessionImpl u, 1.5.0/docs/api/java/lang/String.html">String OP)
    {
        1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation o = 1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation.parse(OP);
        return isClear(u,o);
    }

    /**
     * Same logic in isClearanceInitialized Method
     * @param u
     * @return
     */

    public boolean isClear(UserSessionImpl u, PageContentImpl.1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation op)
    {
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
        {
            logger.warn("Clearances checked for Mr:" + sess.getCookie() + " but clearing for Mr:" + u.getCookie());
            return false;
        }
        else if(sess == null || sess.getCookie() == null || u == null || u.getCookie() == null)
        {
            logger.warn("Clearances are being checked to a user or a session with no cookie identifier");
            return false;
        }
        else if(userSessionClearences != null)
        {
            return userSessionClearences.get(op);
        }
        else
        {
            logger.warn("Checking clearance without sessionClearances initialized, not allowed");
            return false;
        }
    }

    /**
     * Same logic in isClear Method
     * @param u
     * @return
     */

    public boolean isClearanceInitialized(UserSessionImpl u)
    {
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
        {
            return false;
        }
        else if(userSessionClearences == null)
        {
            return false;
        }
        return true;
    }

    @1.5.0/docs/api/java/lang/Override.html">Override
    public void initClearances(UserSessionImpl u, boolean force) {


    }

    public void setUserSessionClearences(Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation, Boolean> userSessionClearences) {
        this.userSessionClearences = userSessionClearences;
    }

    public void setSess(UserSession sess) {
        this.sess = sess;
    }

    @1.5.0/docs/api/java/lang/Override.html">Override
    public 1.5.0/docs/api/java/lang/String.html">String getAccessPolicyClass() {
        if(super.getAccessPolicyClass() == null)
        {
            logger.debug("No AccessPolicyClass in PageContent " + getId() + ":" + getTitle() + " using default:" + PageContentAccessPolicyAcl.class.getName());
            return PageContentAccessPolicyAcl.class.getName();
        }
        return super.getAccessPolicyClass();
    }

    public Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation, Boolean> getClearences() {
        return userSessionClearences;
    }

    public void setClearences(Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation, Boolean> x) {

    }
}