Changeset 27109


Ignore:
Timestamp:
2013-03-21T16:47:19+13:00 (11 years ago)
Author:
sjm84
Message:

Major change to how the files are located. Also added the functionality to retreive all xsl files available to a collection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/DebugService.java

    r27076 r27109  
    44import java.io.FileWriter;
    55import java.io.Serializable;
     6import java.util.ArrayList;
    67import java.util.HashMap;
    78
     
    2930     * The list of services the utility service rack supports *
    3031     *********************************************************/
    31     protected static final String RETRIEVE_TEMPLATE_FROM_XML_FILE = "RetrieveXMLTemplateFromFile";
     32    protected static final String GET_TEMPLATE_FROM_XML_FILE = "GetXMLTemplateFromFile";
    3233    protected static final String SAVE_TEMPLATE_TO_XML_FILE = "SaveXMLTemplateToFile";
    33     protected static final String GET_GSLIB_ELEMENTS_FROM_FILE = "GetGSLIBElementsFromFile";
     34    protected static final String GET_TEMPLATE_LIST_FROM_FILE = "GetTemplateListFromFile";
     35    protected static final String GET_XSLT_FILES_FOR_COLLECTION = "GetXSLTFilesForCollection";
    3436    /*********************************************************/
    3537
    36     String[] services = { RETRIEVE_TEMPLATE_FROM_XML_FILE, SAVE_TEMPLATE_TO_XML_FILE, GET_GSLIB_ELEMENTS_FROM_FILE };
     38    String[] services = { GET_TEMPLATE_FROM_XML_FILE, SAVE_TEMPLATE_TO_XML_FILE, GET_TEMPLATE_LIST_FROM_FILE, GET_XSLT_FILES_FOR_COLLECTION };
    3739
    3840    public boolean configure(Element info, Element extra_info)
     
    7375    }
    7476
    75     protected Element processRetrieveXMLTemplateFromFile(Element request)
    76     {
    77         Element result = GSXML.createBasicResponse(this.doc, RETRIEVE_TEMPLATE_FROM_XML_FILE);
     77    protected Element processGetXMLTemplateFromFile(Element request)
     78    {
     79        Element result = GSXML.createBasicResponse(this.doc, GET_TEMPLATE_FROM_XML_FILE);
    7880
    7981        if (request == null)
    8082        {
    81             GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
     83            GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
    8284            return result;
    8385        }
     
    104106        if (param_list == null)
    105107        {
    106             GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
     108            GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
    107109            return result;
    108110        }
     
    110112        HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
    111113
    112         String filepath = (String) params.get("filePath");
     114        String locationName = (String) params.get("locationName");
     115        String interfaceName = (String) params.get("interfaceName");
     116        String siteName = (String) params.get("siteName");
     117        String collectionName = (String) params.get("collectionName");
     118        String fileName = (String) params.get("fileName");
    113119        String namespace = (String) params.get("namespace");
    114120        String nodeName = (String) params.get("nodename");
     
    127133        else
    128134        {
    129             return result;
    130         }
    131 
    132         File xslFile = new File(filepath);
     135            GSXML.addError(this.doc, result, "A valid namespace was not specified.");
     136            return result;
     137        }
     138
     139        File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
    133140        if (xslFile.exists())
    134141        {
     
    186193                Node currentAttr = attributes.item(k);
    187194                String value = currentAttr.getNodeValue();
    188                 if (value.contains("&") || value.contains("<") || value.contains(">"))
    189                 {
    190                     currentAttr.setNodeValue(value.replace("&", "&amp;amp;").replace("<", "&lt;").replace(">", "&gt;"));
     195                if (value.contains("&") || value.contains("<") || value.contains(">") || value.contains("\""))
     196                {
     197                    currentAttr.setNodeValue(value.replace("&", "&amp;amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;"));
    191198                }
    192199            }
     
    231238        HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
    232239
    233         String filepath = (String) params.get("filePath");
     240        String locationName = (String) params.get("locationName");
     241        String fileName = (String) params.get("fileName");
     242        String interfaceName = (String) params.get("interfaceName");
     243        String siteName = (String) params.get("siteName");
     244        String collectionName = (String) params.get("collectionName");
    234245        String namespace = (String) params.get("namespace");
    235246        String nodeName = (String) params.get("nodename");
     
    253264        }
    254265
    255         File xslFile = new File(filepath);
     266        File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
    256267        if (xslFile.exists())
    257268        {
     
    320331            }
    321332        }
     333        else
     334        {
     335            GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + "File: " + xslFile.getAbsolutePath() + " does not exist", GSXML.ERROR_TYPE_SYNTAX);
     336        }
    322337
    323338        return result;
    324339    }
    325340
    326     protected Element processGetGSLIBElementsFromFile(Element request)
    327     {
    328         Element result = GSXML.createBasicResponse(this.doc, GET_GSLIB_ELEMENTS_FROM_FILE);
     341    protected Element processGetTemplateListFromFile(Element request)
     342    {
     343        Element result = GSXML.createBasicResponse(this.doc, GET_TEMPLATE_LIST_FROM_FILE);
    329344
    330345        if (request == null)
    331346        {
    332             GSXML.addError(this.doc, result, GET_GSLIB_ELEMENTS_FROM_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
     347            GSXML.addError(this.doc, result, GET_TEMPLATE_LIST_FROM_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
    333348            return result;
    334349        }
     
    355370        if (param_list == null)
    356371        {
    357             GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
     372            GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
    358373            return result;
    359374        }
     
    361376        HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
    362377
     378        String locationName = (String) params.get("locationName");
     379        String siteName = (String) params.get("siteName");
     380        String collectionName = (String) params.get("collectionName");
    363381        String interfaceName = (String) params.get("interfaceName");
    364 
    365         String filePath = GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform" + File.separator + "gslib.xsl";
    366         File xslFile = new File(filePath);
     382        String fileName = (String) params.get("fileName");
     383
     384        fileName.replace("/", File.separator);
     385
     386        if (locationName == null || locationName.length() == 0)
     387        {
     388            GSXML.addError(this.doc, result, "Parameter \"locationName\" was null or empty.");
     389            return result;
     390        }
     391
     392        File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
    367393
    368394        if (xslFile.exists())
     
    372398
    373399            Element templateList = this.doc.createElement("templateList");
    374             String templateListString = "[";
    375 
    376             NodeList templateElems = xslDoc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
    377             for (int i = 0; i < templateElems.getLength(); i++)
    378             {
    379                 Element currentElem = (Element) templateElems.item(i);
     400            StringBuilder templateListString = new StringBuilder("[");
     401
     402            NodeList xslTemplateElems = xslDoc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
     403            NodeList gsfTemplateElems = xslDoc.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "template");
     404            for (int i = 0; i < xslTemplateElems.getLength() + gsfTemplateElems.getLength(); i++)
     405            {
     406                Element currentElem = (i < xslTemplateElems.getLength()) ? (Element) xslTemplateElems.item(i) : (Element) gsfTemplateElems.item(i - xslTemplateElems.getLength());
     407                if (!currentElem.hasAttribute(GSXML.NAME_ATT) && !currentElem.hasAttribute(GSXML.MATCH_ATT))
     408                {
     409                    continue;
     410                }
     411
     412                templateListString.append("{");
     413                templateListString.append("\"namespace\":\"" + ((i < xslTemplateElems.getLength()) ? "xsl" : "gsf") + "\",");
    380414                if (currentElem.hasAttribute(GSXML.NAME_ATT))
    381415                {
    382                     templateListString += "\"" + currentElem.getAttribute(GSXML.NAME_ATT) + "\"";
    383                     if (i < templateElems.getLength() - 1)
    384                     {
    385                         templateListString += ",";
    386                     }
    387                 }
    388             }
    389 
    390             templateListString += "]";
    391 
    392             templateList.setTextContent(templateListString);
     416                    templateListString.append("\"name\":\"" + currentElem.getAttribute(GSXML.NAME_ATT) + "\",");
     417                }
     418                else if (currentElem.hasAttribute(GSXML.MATCH_ATT))
     419                {
     420                    templateListString.append("\"match\":\"" + currentElem.getAttribute(GSXML.MATCH_ATT) + "\",");
     421                }
     422                templateListString.deleteCharAt(templateListString.length() - 1);
     423                templateListString.append("},");
     424            }
     425
     426            templateListString.deleteCharAt(templateListString.length() - 1);
     427            templateListString.append("]");
     428
     429            templateList.setTextContent(templateListString.toString());
    393430            result.appendChild(templateList);
    394431        }
     432        else
     433        {
     434            GSXML.addError(this.doc, result, "File: " + xslFile.getAbsolutePath() + " does not exist");
     435            return result;
     436        }
    395437
    396438        return result;
    397439    }
     440
     441    protected Element processGetXSLTFilesForCollection(Element request)
     442    {
     443        Element result = GSXML.createBasicResponse(this.doc, GET_XSLT_FILES_FOR_COLLECTION);
     444
     445        if (request == null)
     446        {
     447            GSXML.addError(this.doc, result, GET_XSLT_FILES_FOR_COLLECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
     448            return result;
     449        }
     450
     451        UserContext context = new UserContext(request);
     452        boolean found = false;
     453        for (String group : context.getGroups())
     454        {
     455            if (group.equals("administrator"))
     456            {
     457                found = true;
     458            }
     459        }
     460
     461        if (!found)
     462        {
     463            GSXML.addError(this.doc, result, "This user does not have the required permissions to perform this action.");
     464            return result;
     465        }
     466
     467        // Get the parameters of the request
     468        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     469
     470        if (param_list == null)
     471        {
     472            GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
     473            return result;
     474        }
     475
     476        HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
     477
     478        String interfaceName = (String) params.get("interfaceName");
     479        String siteName = (String) params.get("siteName");
     480        String collectionName = (String) params.get("collectionName");
     481
     482        Element fileList = this.doc.createElement("fileListJSON");
     483        StringBuilder fileListString = new StringBuilder("[");
     484
     485        String[] placesToCheck = new String[] { GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform", //INTERFACE FILE PATH
     486        GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "transform", //SITE FILE PATH
     487        GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "transform", //COLLECTION FILE PATH
     488        GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "etc" //COLLECTION CONFIG FILE PATH
     489        };
     490
     491        String[] shortPlaceNames = new String[] { "interface", "site", "collection", "collectionConfig" };
     492
     493        for (int i = 0; i < placesToCheck.length; i++)
     494        {
     495            ArrayList<File> xslFiles = getXSLTFilesFromDirectoryRecursive(new File(placesToCheck[i]));
     496            for (File f : xslFiles)
     497            {
     498                fileListString.append("{\"location\":\"" + shortPlaceNames[i] + "\",\"path\":\"" + f.getAbsolutePath().replace(placesToCheck[i] + File.separator, "") + "\"},");
     499            }
     500        }
     501        fileListString.deleteCharAt(fileListString.length() - 1); //Remove the last , character
     502        fileListString.append("]");
     503
     504        fileList.setTextContent(fileListString.toString());
     505        result.appendChild(fileList);
     506
     507        return result;
     508    }
     509
     510    protected File createFileFromLocation(String location, String filename, String interfaceName, String siteName, String collectionName)
     511    {
     512        String filePath = "";
     513        if (location.equals("interface"))
     514        {
     515            filePath = GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform" + File.separator + filename;
     516        }
     517        else if (location.equals("site"))
     518        {
     519            filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "transform" + File.separator + filename;
     520        }
     521        else if (location.equals("collection"))
     522        {
     523            filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "transform" + File.separator + filename;
     524        }
     525        else if (location.equals("collectionConfig"))
     526        {
     527            filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "etc" + File.separator + filename;
     528        }
     529
     530        return new File(filePath);
     531    }
     532
     533    protected ArrayList<File> getXSLTFilesFromDirectoryRecursive(File dir)
     534    {
     535        ArrayList<File> result = new ArrayList<File>();
     536
     537        if (dir != null && dir.exists() && dir.isDirectory())
     538        {
     539            for (File f : dir.listFiles())
     540            {
     541                if (f.isDirectory())
     542                {
     543                    result.addAll(getXSLTFilesFromDirectoryRecursive(f));
     544                }
     545                else if (f.getName().endsWith(".xsl") || f.getName().endsWith(".xml"))
     546                {
     547                    result.add(f);
     548                }
     549            }
     550        }
     551
     552        return result;
     553    }
    398554}
Note: See TracChangeset for help on using the changeset viewer.