コード例 #1
0
ファイル: GetRecord.java プロジェクト: bcourtine/OAICat
  /**
   * Construct the xml response on the server-side.
   *
   * @param context the servlet context
   * @param request the servlet request
   * @return a String containing the XML response
   * @throws OAIInternalServerError an http 500 status error occurred
   */
  public static String construct(
      HashMap context,
      HttpServletRequest request,
      HttpServletResponse response,
      Transformer serverTransformer)
      throws OAIInternalServerError, TransformerException {
    Properties properties = (Properties) context.get("OAIHandler.properties");
    AbstractCatalog abstractCatalog = (AbstractCatalog) context.get("OAIHandler.catalog");
    String baseURL = properties.getProperty("OAIHandler.baseURL");
    if (baseURL == null) {
      try {
        baseURL = request.getRequestURL().toString();
      } catch (java.lang.NoSuchMethodError f) {
        baseURL = request.getRequestURL().toString();
      }
    }
    StringBuilder sb = new StringBuilder();
    String identifier = request.getParameter("identifier");
    String metadataPrefix = request.getParameter("metadataPrefix");
    LOGGER.debug("GetRecord.constructGetRecord: identifier=" + identifier);
    LOGGER.debug("GetRecord.constructGetRecord: metadataPrefix=" + metadataPrefix);
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
    String styleSheet = properties.getProperty("OAIHandler.styleSheet");
    if (styleSheet != null) {
      sb.append("<?xml-stylesheet type=\"text/xsl\" href=\"");
      sb.append(styleSheet);
      sb.append("\"?>");
    }
    sb.append("<OAI-PMH xmlns=\"http://www.openarchives.org/OAI/2.0/\"");
    sb.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    String extraXmlns = properties.getProperty("OAIHandler.extraXmlns");
    if (extraXmlns != null) {
      sb.append(" ").append(extraXmlns);
    }
    sb.append(" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/");
    sb.append(" http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd\">");
    sb.append("<responseDate>");
    sb.append(createResponseDate(new Date()));
    sb.append("</responseDate>");

    Crosswalks crosswalks = abstractCatalog.getCrosswalks();
    try {
      if (metadataPrefix == null
          || metadataPrefix.length() == 0
          || identifier == null
          || identifier.length() == 0
          || hasBadArguments(request, validParamNames.iterator(), validParamNames)) {
        throw new BadArgumentException();
      } else if (!crosswalks.containsValue(metadataPrefix)) {
        throw new CannotDisseminateFormatException(metadataPrefix);
      } else {
        String record = abstractCatalog.getRecord(identifier, metadataPrefix);
        if (record != null) {
          sb.append(getRequestElement(request, validParamNames, baseURL));
          sb.append("<GetRecord>");
          sb.append(record);
          sb.append("</GetRecord>");
        } else {
          throw new IdDoesNotExistException(identifier);
        }
      }
    } catch (BadArgumentException e) {
      sb.append("<request verb=\"GetRecord\">");
      sb.append(baseURL);
      sb.append("</request>");
      sb.append(e.getMessage());
    } catch (CannotDisseminateFormatException e) {
      sb.append(getRequestElement(request, validParamNames, baseURL));
      sb.append(e.getMessage());
    } catch (IdDoesNotExistException e) {
      sb.append(getRequestElement(request, validParamNames, baseURL));
      sb.append(e.getMessage());
    }
    sb.append("</OAI-PMH>");
    return render(response, "text/xml; charset=UTF-8", sb.toString(), serverTransformer);
  }
コード例 #2
0
ファイル: ListIdentifiers.java プロジェクト: datacite/oaip
  public static String construct(
      HashMap context,
      HttpServletRequest request,
      HttpServletResponse response,
      Transformer serverTransformer)
      throws OAIInternalServerError, TransformerException {
    Properties properties = (Properties) context.get("OAIHandler.properties");
    AbstractCatalog abstractCatalog = (AbstractCatalog) context.get("OAIHandler.catalog");
    boolean xmlEncodeSetSpec =
        "true".equalsIgnoreCase(properties.getProperty("OAIHandler.xmlEncodeSetSpec"));
    boolean urlEncodeSetSpec =
        !"false".equalsIgnoreCase(properties.getProperty("OAIHandler.urlEncodeSetSpec"));
    String baseURL = properties.getProperty("OAIHandler.baseURL");
    if (baseURL == null) {
      try {
        baseURL = request.getRequestURL().toString();
      } catch (java.lang.NoSuchMethodError f) {
        baseURL = HttpUtils.getRequestURL(request).toString();
      }
    }
    StringBuffer sb = new StringBuffer();
    String oldResumptionToken = request.getParameter("resumptionToken");
    String metadataPrefix = request.getParameter("metadataPrefix");

    if (metadataPrefix != null && metadataPrefix.length() == 0) metadataPrefix = null;

    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
    String styleSheet = properties.getProperty("OAIHandler.styleSheet");
    if (styleSheet != null) {
      sb.append("<?xml-stylesheet type=\"text/xsl\" href=\"");
      sb.append(styleSheet);
      sb.append("\"?>");
    }
    sb.append("<OAI-PMH xmlns=\"http://www.openarchives.org/OAI/2.0/\"");
    sb.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    sb.append(" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/");
    sb.append(" http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd\">");
    sb.append("<responseDate>");
    sb.append(createResponseDate(new Date()));
    sb.append("</responseDate>");
    //  sb.append("<requestURL>");
    //  sb.append(getRequestURL(request));
    //  sb.append("</requestURL>");

    if (!abstractCatalog.isHarvestable()) {
      sb.append("<request verb=\"ListIdentifiers\">");
      sb.append(baseURL);
      sb.append("</request>");
      sb.append("<error code=\"badArgument\">Database is unavailable for harvesting</error>");
    } else {
      ArrayList validParamNames = null;
      ArrayList requiredParamNames = null;
      Map listIdentifiersMap = null;
      if (oldResumptionToken == null) {
        validParamNames = validParamNames1;
        requiredParamNames = requiredParamNames1;
        String from = request.getParameter("from");
        String until = request.getParameter("until");
        try {
          if (from != null && from.length() > 0 && from.length() < 10) {
            throw new BadArgumentException();
          }
          if (until != null && until.length() > 0 && until.length() < 10) {
            throw new BadArgumentException();
          }
          if (from != null && until != null && from.length() != until.length()) {
            throw new BadArgumentException();
          }

          if (from == null || from.length() == 0) {
            // from = "0001-01-01";
            from = Constants.DateTime.MIN_FROM_DATE;
            // from = "0000";
          }
          /*          else{
              from = abstractCatalog.toFinestFrom(from);
          }*/

          if (until == null || until.length() == 0) {
            // until = "9999-12-31";
            until = Constants.DateTime.MAX_TO_DATE;
            // until = "9999";
          }
          /*          else{
              until = abstractCatalog.toFinestUntil(until);
          }
          */

          from = abstractCatalog.toFinestFrom(from);
          until = abstractCatalog.toFinestUntil(until);

          if (from.compareTo(until) > 0) throw new BadArgumentException();
          String set = request.getParameter("set");
          if (set != null) {
            if (set.length() == 0) set = null;
            else if (urlEncodeSetSpec) set = set.replace(' ', '+');
          }
          Crosswalks crosswalks = abstractCatalog.getCrosswalks();
          if (metadataPrefix == null) {
            throw new BadArgumentException();
          }

          if (!crosswalks.containsValue(metadataPrefix)) {
            throw new CannotDisseminateFormatException(metadataPrefix);
          } else {
            listIdentifiersMap = abstractCatalog.listIdentifiers(from, until, set, metadataPrefix);
          }
        } catch (NoItemsMatchException e) {
          sb.append(getRequestElement(request, validParamNames, baseURL, xmlEncodeSetSpec));
          sb.append(e.getMessage());
        } catch (BadArgumentException e) {
          sb.append("<request verb=\"ListIdentifiers\">");
          //          sb.append(HttpUtils.getRequestURL(request));
          sb.append(baseURL);
          sb.append("</request>");
          sb.append(e.getMessage());
          //      } catch (BadGranularityException e) {
          //          sb.append(getRequestElement(request));
          //          sb.append(e.getMessage());
        } catch (CannotDisseminateFormatException e) {
          sb.append(getRequestElement(request, validParamNames, baseURL, xmlEncodeSetSpec));
          sb.append(e.getMessage());
        } catch (NoSetHierarchyException e) {
          sb.append(getRequestElement(request, validParamNames, baseURL, xmlEncodeSetSpec));
          sb.append(e.getMessage());
        }
      } else {
        validParamNames = validParamNames2;
        requiredParamNames = requiredParamNames2;
        if (hasBadArguments(request, requiredParamNames.iterator(), validParamNames)) {
          sb.append(getRequestElement(request, validParamNames, baseURL, xmlEncodeSetSpec));
          sb.append(new BadArgumentException().getMessage());
        } else {
          try {
            listIdentifiersMap = abstractCatalog.listIdentifiers(oldResumptionToken);
          } catch (BadResumptionTokenException e) {
            sb.append(getRequestElement(request, validParamNames, baseURL, xmlEncodeSetSpec));
            sb.append(e.getMessage());
          }
        }
      }

      if (listIdentifiersMap != null) {
        sb.append(getRequestElement(request, validParamNames, baseURL, xmlEncodeSetSpec));
        if (hasBadArguments(request, requiredParamNames.iterator(), validParamNames)) {
          sb.append(new BadArgumentException().getMessage());
        } else {
          sb.append("<ListIdentifiers>");
          Iterator identifiers = (Iterator) listIdentifiersMap.get("headers");
          while (identifiers.hasNext()) {
            sb.append((String) identifiers.next());
          }

          Map newResumptionMap = (Map) listIdentifiersMap.get("resumptionMap");
          if (newResumptionMap != null) {
            String newResumptionToken = (String) newResumptionMap.get("resumptionToken");
            String expirationDate = (String) newResumptionMap.get("expirationDate");
            String completeListSize = (String) newResumptionMap.get("completeListSize");
            String cursor = (String) newResumptionMap.get("cursor");
            sb.append("<resumptionToken");
            if (expirationDate != null) {
              sb.append(" expirationDate=\"");
              sb.append(expirationDate);
              sb.append("\"");
            }
            if (completeListSize != null) {
              sb.append(" completeListSize=\"");
              sb.append(completeListSize);
              sb.append("\"");
            }
            if (cursor != null) {
              sb.append(" cursor=\"");
              sb.append(cursor);
              sb.append("\"");
            }
            sb.append(">");
            sb.append(newResumptionToken);
            sb.append("</resumptionToken>");
          } else if (oldResumptionToken != null) {
            sb.append("<resumptionToken />");
          }
          sb.append("</ListIdentifiers>");
        }
      }
    }
    sb.append("</OAI-PMH>");
    if (debug) {
      log.info("ListIdentifiers.constructListIdentifiers: returning: " + sb.toString());
    }
    return render(response, "text/xml; charset=UTF-8", sb.toString(), serverTransformer);
  }