Ejemplo n.º 1
0
 /** Sets Tesseract's internal parameters. */
 private void setTessVariables() {
   Enumeration<?> em = prop.propertyNames();
   while (em.hasMoreElements()) {
     String key = (String) em.nextElement();
     api.TessBaseAPISetVariable(handle, key, prop.getProperty(key));
   }
 }
Ejemplo n.º 2
0
  private void loadConfiguration() throws RuntimeConfigException {
    trace(String.format("Loading configuration from [%s]", configFilePath));
    try {
      InputStream stream = new FileInputStream(configFilePath);
      // InputStream stream1 = new FileInputStream("config/ocsswws.config");
      try {
        Properties fileProperties = new Properties();
        fileProperties.load(stream);
        // @todo check tests - code was not backward compatible with Java 5
        // so i changed it - but this is not the only place of uncompatibilty
        // add default properties so that they override file properties
        // Set<String> propertyNames = fileProperties.stringPropertyNames();
        //                for (String propertyName : propertyNames) {
        //                    String propertyValue = fileProperties.getProperty(propertyName);
        //                    if (!isPropertySet(propertyName)) {
        //                        setProperty(propertyName, propertyValue);
        //                        trace(String.format("Configuration property [%s] added",
        // propertyName));
        //                    } else {
        //                        trace(String.format("Configuration property [%s] ignored",
        // propertyName));
        //                    }
        //                }

        Enumeration<?> enumeration = fileProperties.propertyNames();
        while (enumeration.hasMoreElements()) {
          final Object key = enumeration.nextElement();
          if (key instanceof String) {
            final Object value = fileProperties.get(key);
            if (value instanceof String) {
              final String keyString = (String) key;
              String propertyValue = fileProperties.getProperty(keyString);
              if (!isPropertySet(keyString)) {
                setProperty(keyString, propertyValue);
                trace(String.format("Configuration property [%s] added", keyString));
              } else {
                trace(String.format("Configuration property [%s] ignored", keyString));
              }
            }
          }
        }
      } finally {
        stream.close();
      }
    } catch (IOException e) {
      throw new RuntimeConfigException(
          String.format("Failed to load configuration [%s]", configFilePath), e);
    }
  }
Ejemplo n.º 3
0
 /**
  * Helper method to convert properties to string.
  *
  * @param prop a <code>Properties</code> value
  * @return a <code>String</code> value
  */
 public static String convertPropsToString(Properties prop) {
   if (prop == null) {
     return "{null}";
   }
   StringBuffer strBuf = new StringBuffer("{ ");
   for (Enumeration enum1 = prop.propertyNames(); enum1.hasMoreElements(); ) {
     Object obj = enum1.nextElement();
     strBuf.append("[ ").append(obj).append("->");
     Object val = prop.getProperty((String) obj);
     if (val == null) strBuf.append("null");
     else strBuf.append((String) val);
     strBuf.append(" ] ");
   }
   strBuf.append("}");
   return strBuf.toString();
 }
  @Override
  public Response serve(String uri, String method, Properties header, Properties parms) {
    if (!uri.equals("/mediaserver")) {
      return super.serve(uri, method, header, parms); // this way we can
      // also serve up
      // normal files and
      // content
    }

    logger.fine(method + " '" + uri + "' ");

    Enumeration<?> e = header.propertyNames();
    while (e.hasMoreElements()) {
      String value = (String) e.nextElement();
      logger.fine("  HDR: '" + value + "' = '" + header.getProperty(value) + "'");
    }
    e = parms.propertyNames();
    while (e.hasMoreElements()) {
      String value = (String) e.nextElement();
      logger.fine("  PRM: '" + value + "' = '" + parms.getProperty(value) + "'");
    }

    // TODO: check the actual path...

    final String mediaPath = parms.getProperty("media");
    final String outputFormatStr = parms.getProperty("format");
    final String mimeType = parms.getProperty("mime");
    logger.info("requested media: " + mediaPath);
    logger.info("requested mime type: " + mimeType);
    if (mediaPath == null)
      return new Response(HTTP_FORBIDDEN, "text/plain", "mediaPath parameter not specified");
    if (mimeType == null)
      return new Response(HTTP_FORBIDDEN, "text/plain", "mimeType parameter not specified");

    // TODO: if we aren't performing any transcoding, just serve the file up
    // directly.
    // TODO: capture sources need to be treated as singletons, with some
    // kind of broadcasting/cloning to ensure
    // that multiple connections can be made.

    final String serverSideUrlStr = mediaPath; // URLUtils.createUrlStr(new
    // File(mediaPath)); // TODO:
    // enforce that we can't just
    // serve up anything anywhere
    final ContentDescriptor outputContentDescriptor =
        new FileTypeDescriptor(ContentDescriptor.mimeTypeToPackageName(mimeType));

    final Format outputFormat;
    if (outputFormatStr == null) {
      outputFormat = null;
    } else {
      try {
        outputFormat = FormatArgUtils.parse(outputFormatStr);
      } catch (ParseException e1) {
        logger.log(Level.WARNING, "" + e1, e1);
        return new Response(HTTP_FORBIDDEN, "text/plain", "" + e1);
      }
    }

    logger.info("serverSideUrlStr: " + serverSideUrlStr);
    logger.info("outputContentDescriptor: " + outputContentDescriptor);
    logger.info("outputFormat: " + outputFormat);

    final InputStream is;
    try {
      is = getInputStream(serverSideUrlStr, outputFormat, outputContentDescriptor);
    } catch (Exception e1) {
      return new Response(HTTP_FORBIDDEN, "text/plain", "" + e1);
    }

    final String responseMimeType;
    // workaround for the problem that the multipart/x-mixed-replace
    // boundary is not stored anywhere.
    // this assumes that if we are serving multipart/x-mixed-replace data,
    // that MultipartMixedReplaceMux is being used.
    if (mimeType.equals("multipart/x-mixed-replace"))
      responseMimeType = mimeType + ";boundary=" + MultipartMixedReplaceMux.BOUNDARY;
    else responseMimeType = mimeType;
    logger.info("Response mime type: " + responseMimeType);
    return new Response(HTTP_OK, responseMimeType, is);
  }