Example #1
1
  /** @see java.net.URLStreamHandler#openConnection(URL) */
  protected URLConnection openConnection(URL u) throws IOException {

    try {

      URL otherURL = new URL(u.getFile());
      String host = otherURL.getHost();
      String protocol = otherURL.getProtocol();
      int port = otherURL.getPort();
      String file = otherURL.getFile();
      String query = otherURL.getQuery();
      // System.out.println("Trying to load: " + u.toExternalForm());
      String internalFile = null;
      if (file.indexOf("!/") != -1) {
        internalFile = file.substring(file.indexOf("!/") + 2);
        file = file.substring(0, file.indexOf("!/"));
      }
      URL subURL = new URL(protocol, host, port, file);
      File newFile;
      if (cacheList.containsKey(subURL.toExternalForm())) {
        newFile = cacheList.get(subURL.toExternalForm());
      } else {
        InputStream is = (InputStream) subURL.getContent();
        String update = subURL.toExternalForm().replace(":", "_");
        update = update.replace("/", "-");
        File f = File.createTempFile("pack", update);
        f.deleteOnExit();
        FileOutputStream fostream = new FileOutputStream(f);
        copyStream(new BufferedInputStream(is), fostream);

        // Unpack
        newFile = File.createTempFile("unpack", update);
        newFile.deleteOnExit();
        fostream = new FileOutputStream(newFile);
        BufferedOutputStream bos = new BufferedOutputStream(fostream, 50 * 1024);
        JarOutputStream jostream = new JarOutputStream(bos);
        Unpacker unpacker = Pack200.newUnpacker();
        long start = System.currentTimeMillis();

        unpacker.unpack(f, jostream);
        printMessage(
            "Unpack of " + update + " took " + (System.currentTimeMillis() - start) + "ms");
        jostream.close();
        fostream.close();
        cacheList.put(subURL.toExternalForm(), newFile);
      }
      if (internalFile != null) {

        URL directoryURL = new URL("jar:" + newFile.toURL().toExternalForm() + "!/" + internalFile);
        printMessage("Using DirectoryURL:" + directoryURL);
        return directoryURL.openConnection();
      } else return newFile.toURL().openConnection();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    printMessage("Returning null for: " + u.toExternalForm());
    return null;
  }
  /**
   * Convert the document from a PDF to a lucene document.
   *
   * @param url A url to a PDF document.
   * @return The PDF converted to a lucene document.
   * @throws IOException If there is an error while converting the document.
   */
  public Document convertDocument(URL url) throws IOException {
    Document document = new Document();
    URLConnection connection = url.openConnection();
    connection.connect();
    // Add the url as a field named "url". Use an UnIndexed field, so
    // that the url is just stored with the document, but is not searchable.
    addUnindexedField(document, "url", url.toExternalForm());

    // Add the last modified date of the file a field named "modified". Use a
    // Keyword field, so that it's searchable, but so that no attempt is made
    // to tokenize the field into words.
    addKeywordField(document, "modified", timeToString(connection.getLastModified()));

    String uid = createUID(url, connection.getLastModified());

    // Add the uid as a field, so that index can be incrementally maintained.
    // This field is not stored with document, it is indexed, but it is not
    // tokenized prior to indexing.
    addUnstoredKeywordField(document, "uid", uid);

    InputStream input = null;
    try {
      input = connection.getInputStream();
      addContent(document, input, url.toExternalForm());
    } finally {
      if (input != null) {
        input.close();
      }
    }

    // return the document
    return document;
  }
Example #3
0
 protected ResolvedResource getArtifactRef(Artifact artifact, Date date) {
   IvyContext.getContext().set(getName() + ".artifact", artifact);
   try {
     ResolvedResource ret = findArtifactRef(artifact, date);
     if (ret == null && artifact.getUrl() != null) {
       URL url = artifact.getUrl();
       Message.verbose("\tusing url for " + artifact + ": " + url);
       logArtifactAttempt(artifact, url.toExternalForm());
       Resource resource;
       if ("file".equals(url.getProtocol())) {
         File f;
         try {
           f = new File(new URI(url.toExternalForm()));
         } catch (URISyntaxException e) {
           // unexpected, try to get the best of it
           f = new File(url.getPath());
         }
         resource = new FileResource(new FileRepository(), f);
       } else {
         resource = new URLResource(url);
       }
       ret = new ResolvedResource(resource, artifact.getModuleRevisionId().getRevision());
     }
     return ret;
   } finally {
     IvyContext.getContext().set(getName() + ".artifact", null);
   }
 }
  /**
   * Initializes the URL that will be used for web service access
   *
   * @param deploymentId Deployment ID
   * @param url URL of the server instance
   * @return An URL that can be used for the web services
   */
  private URL initializeServicesUrl(URL url, String servicePrefix) {
    if (url == null) {
      throw new IllegalArgumentException("The url may not be empty or null.");
    }
    try {
      url.toURI();
    } catch (URISyntaxException urise) {
      throw new IllegalArgumentException(
          "URL (" + url.toExternalForm() + ") is incorrectly formatted: " + urise.getMessage(),
          urise);
    }

    String urlString = url.toExternalForm();
    if (!urlString.endsWith("/")) {
      urlString += "/";
    }
    urlString += servicePrefix;

    URL serverPlusServicePrefixUrl;
    try {
      serverPlusServicePrefixUrl = new URL(urlString);
    } catch (MalformedURLException murle) {
      throw new IllegalArgumentException(
          "URL (" + url.toExternalForm() + ") is incorrectly formatted: " + murle.getMessage(),
          murle);
    }

    return serverPlusServicePrefixUrl;
  }
  /** Used for redirections or when discovering sitemap URLs * */
  private void handleOutlink(Tuple t, URL sURL, String newUrl, Metadata sourceMetadata) {
    // build an absolute URL
    try {
      URL tmpURL = URLUtil.resolveURL(sURL, newUrl);
      newUrl = tmpURL.toExternalForm();
    } catch (MalformedURLException e) {
      LOG.debug("MalformedURLException on {} or {}: {}", sURL.toExternalForm(), newUrl, e);
      return;
    }

    // apply URL filters
    if (this.urlFilters != null) {
      newUrl = this.urlFilters.filter(sURL, sourceMetadata, newUrl);
    }

    // filtered
    if (newUrl == null) {
      return;
    }

    Metadata metadata =
        metadataTransfer.getMetaForOutlink(newUrl, sURL.toExternalForm(), sourceMetadata);

    // TODO check that hasn't exceeded max number of redirections

    _collector.emit(
        com.digitalpebble.storm.crawler.Constants.StatusStreamName,
        t,
        new Values(newUrl, metadata, Status.DISCOVERED));
  }
Example #6
0
  @Override
  public NodeTypeIterator registerNodeTypes(URL url, boolean allowUpdate)
      throws IOException, RepositoryException {
    String content = IoUtil.read(url.openStream());
    if (content.startsWith("<?xml")) {
      // This is Jackrabbit XML format ...
      return registerNodeTypes(
          importFromXml(new InputSource(new StringReader(content))), allowUpdate);
    }
    // Assume this is CND format ...
    CndImporter importer = new CndImporter(context(), true);
    Problems problems = new SimpleProblems();
    importer.importFrom(content, problems, url.toExternalForm());

    // Check for (and report) any problems ...
    if (problems.hasProblems()) {
      // There are errors and/or warnings, so report them ...
      String summary = messageFrom(problems);
      if (problems.hasErrors()) {
        String msg = JcrI18n.errorsParsingNodeTypeDefinitions.text(url.toExternalForm(), summary);
        throw new RepositoryException(msg);
      }
      // Otherwise, there are warnings, so log them ...
      I18n msg = JcrI18n.warningsParsingNodeTypeDefinitions;
      Logger.getLogger(getClass()).warn(msg, url.toExternalForm(), summary);
    }

    // Register the node types ...
    return registerNodeTypes(importer.getNodeTypeDefinitions(), allowUpdate);
  }
Example #7
0
 /**
  * 获取类的class文件位置的URL
  *
  * @param cls
  * @return
  */
 private static URL getClassLocationURL(final Class cls) {
   if (cls == null) throw new IllegalArgumentException("null input: cls");
   URL result = null;
   final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
   final ProtectionDomain pd = cls.getProtectionDomain();
   if (pd != null) {
     final CodeSource cs = pd.getCodeSource();
     if (cs != null) result = cs.getLocation();
     if (result != null) {
       if ("file".equals(result.getProtocol())) {
         try {
           if (result.toExternalForm().endsWith(".jar")
               || result.toExternalForm().endsWith(".zip"))
             result =
                 new URL(
                     "jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource));
           else if (new File(result.getFile()).isDirectory())
             result = new URL(result, clsAsResource);
         } catch (MalformedURLException ignore) {
         }
       }
     }
   }
   if (result == null) {
     final ClassLoader clsLoader = cls.getClassLoader();
     result =
         clsLoader != null
             ? clsLoader.getResource(clsAsResource)
             : ClassLoader.getSystemResource(clsAsResource);
   }
   return result;
 }
 /**
  * Returns the location of the Javadoc.
  *
  * @param element whose Javadoc location has to be found
  * @param isBinary <code>true</code> if the Java element is from a binary container
  * @return the location URL of the Javadoc or <code>null</code> if the location cannot be found
  * @throws JavaModelException thrown when the Java element cannot be accessed
  * @since 3.9
  */
 public static String getBaseURL(IJavaElement element, boolean isBinary)
     throws JavaModelException {
   if (isBinary) {
     // Source attachment usually does not include Javadoc resources
     // => Always use the Javadoc location as base:
     URL baseURL = JavaUI.getJavadocLocation(element, false);
     if (baseURL != null) {
       if (baseURL.getProtocol().equals(JAR_PROTOCOL)) {
         // It's a JarURLConnection, which is not known to the browser widget.
         // Let's start the help web server:
         URL baseURL2 =
             PlatformUI.getWorkbench().getHelpSystem().resolve(baseURL.toExternalForm(), true);
         if (baseURL2 != null) { // can be null if org.eclipse.help.ui is not available
           baseURL = baseURL2;
         }
       }
       return baseURL.toExternalForm();
     }
   } else {
     IResource resource = element.getResource();
     if (resource != null) {
       /*
        * Too bad: Browser widget knows nothing about EFS and custom URL handlers,
        * so IResource#getLocationURI() does not work in all cases.
        * We only support the local file system for now.
        * A solution could be https://bugs.eclipse.org/bugs/show_bug.cgi?id=149022 .
        */
       IPath location = resource.getLocation();
       if (location != null) return location.toFile().toURI().toString();
     }
   }
   return null;
 }
  /**
   * Check if the resource is an eDoc.
   *
   * @param uri - the resource's URI.
   * @return true if the resource is an (KOBV) eDoc.
   */
  public static boolean isEDoc(String uri) {
    // test local file system
    File f = new File(uri);

    if (f.getParentFile().getName().equals("pdf")
        && new File(f.getParentFile().getParentFile(), "index.html").exists()) {
      return true;
    } else { // test HTTP
      try {
        URL url = new URL(uri);
        int pos = url.toExternalForm().lastIndexOf("/pdf");
        if (pos != -1) {
          String newUrl = url.toExternalForm().substring(0, pos) + "/index.html";

          URL indexUrl = new URL(newUrl);
          @SuppressWarnings("unused")
          URLConnection conn = indexUrl.openConnection();

          return true;
        }
        return false;
      } catch (MalformedURLException e) {
        return false;
      } catch (IOException e) {
        return false;
      }
    }
  }
  /**
   * Tests access to the given web application URL.
   *
   * @param webAppURL
   * @throws Exception
   */
  private void testAccess(@ArquillianResource URL webAppURL) throws Exception {
    final URL servletURL =
        new URL(webAppURL.toExternalForm() + SimpleSecuredServlet.SERVLET_PATH.substring(1));
    LOGGER.info("Testing successful authentication - " + servletURL);
    assertEquals(
        "Expected response body doesn't match the returned one.",
        SimpleSecuredServlet.RESPONSE_BODY,
        Utils.makeCallWithBasicAuthn(servletURL, "jduke", "theduke", 200));

    LOGGER.info("Testing failed authentication - " + servletURL);
    Utils.makeCallWithBasicAuthn(servletURL, "anil", "theduke", 401);
    Utils.makeCallWithBasicAuthn(servletURL, "jduke", "anil", 401);
    Utils.makeCallWithBasicAuthn(servletURL, "anil", "anil", 401);

    LOGGER.info("Testing failed authorization - " + servletURL);
    Utils.makeCallWithBasicAuthn(servletURL, "tester", "password", 403);

    final URL unprotectedURL =
        new URL(webAppURL.toExternalForm() + SimpleServlet.SERVLET_PATH.substring(1));
    LOGGER.info("Testing access to unprotected resource - " + unprotectedURL);
    assertEquals(
        "Expected response body doesn't match the returned one.",
        SimpleServlet.RESPONSE_BODY,
        Utils.makeCallWithBasicAuthn(unprotectedURL, null, null, 200));
  }
Example #11
0
  public static StringBuffer _parseItem(
      Log log,
      IndexWriter writer,
      String root,
      URL url,
      List urlsDone,
      String[] extensions,
      boolean recurse,
      int deep,
      long timeout)
      throws IOException {
    try {
      url = translateURL(url);
      if (urlsDone.contains(url.toExternalForm())) return null;
      urlsDone.add(url.toExternalForm());

      StringBuffer content = new StringBuffer();
      Document doc = toDocument(content, writer, root, url, timeout);

      if (doc == null) return null;
      if (writer != null) writer.addDocument(doc);

      // Test
      /*Resource dir = ResourcesImpl.getFileResourceProvider().getResource("/Users/mic/Temp/leeway3/");
      if(!dir.isDirectory())dir.mkdirs();
      Resource file=dir.getRealResource(url.toExternalForm().replace("/", "_"));
      IOUtil.write(file, content.toString(), "UTF-8", false);*/

      info(log, url.toExternalForm());
      return content;
    } catch (IOException ioe) {
      error(log, url.toExternalForm(), ioe);
      throw ioe;
    }
  }
Example #12
0
 /**
  * Create an OAR
  *
  * @param url The URL for the OperationalString Archive
  * @throws OARException If the manifest cannot be read
  * @throws IllegalArgumentException If the url is null
  */
 public OAR(URL url) throws OARException {
   if (url == null) throw new IllegalArgumentException("url cannot be null");
   JarFile jar = null;
   try {
     URL oarURL;
     if (url.getProtocol().equals("jar")) {
       oarURL = url;
     } else {
       StringBuilder sb = new StringBuilder();
       sb.append("jar:").append(url.toExternalForm()).append("!/");
       oarURL = new URL(sb.toString());
     }
     JarURLConnection conn = (JarURLConnection) oarURL.openConnection();
     jar = conn.getJarFile();
     Manifest man = jar.getManifest();
     getManifestAttributes(man);
     loadRepositories(jar);
     jar.close();
     this.url = url;
   } catch (Exception e) {
     throw new OARException("Problem processing " + url.toExternalForm(), e);
   } finally {
     try {
       if (jar != null) jar.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
Example #13
0
  /**
   * Get an absolute URL from a URL attribute that may be relative (i.e. an <code>&lt;a href></code>
   * or <code>&lt;img src></code>).
   *
   * <p>E.g.: <code>String absUrl = linkEl.absUrl("href");</code>
   *
   * <p>If the attribute value is already absolute (i.e. it starts with a protocol, like <code>
   * http://</code> or <code>https://</code> etc), and it successfully parses as a URL, the
   * attribute is returned directly. Otherwise, it is treated as a URL relative to the element's
   * {@link #baseUri}, and made absolute using that.
   *
   * <p>As an alternate, you can use the {@link #attr} method with the <code>abs:</code> prefix,
   * e.g.: <code>String absUrl = linkEl.attr("abs:href");</code>
   *
   * @param attributeKey The attribute key
   * @return An absolute URL if one could be made, or an empty string (not null) if the attribute
   *     was missing or could not be made successfully into a URL.
   * @see #attr
   * @see java.net.URL#URL(java.net.URL, String)
   */
  public String absUrl(String attributeKey) {
    Validate.notEmpty(attributeKey);

    String relUrl = attr(attributeKey);
    if (!hasAttr(attributeKey)) {
      return ""; // nothing to make absolute with
    } else {
      URL base;
      try {
        try {
          base = new URL(baseUri);
        } catch (MalformedURLException e) {
          // the base is unsuitable, but the attribute may be abs on its own, so try that
          URL abs = new URL(relUrl);
          return abs.toExternalForm();
        }
        // workaround: java resolves '//path/file + ?foo' to '//path/?foo', not '//path/file?foo' as
        // desired
        if (relUrl.startsWith("?")) relUrl = base.getPath() + relUrl;
        URL abs = new URL(base, relUrl);
        return abs.toExternalForm();
      } catch (MalformedURLException e) {
        return "";
      }
    }
  }
Example #14
0
    /**
     * Allow the application to resolve external entities.
     *
     * <p>The strategy we follow is:
     *
     * <p>We first check whether the DTD points to a well defined URI, and resolve to our internal
     * DTDs.
     *
     * <p>If the systemId points to a file, then we attempt to read the DTD from the filesystem, in
     * case they've been modified by the user. Otherwise, we fallback to the built-in DTDs inside
     * jPOS.
     *
     * <p>
     *
     * @param publicId The public identifier of the external entity being referenced, or null if
     *     none was supplied.
     * @param systemId The system identifier of the external entity being referenced.
     * @return An InputSource object describing the new input source, or null to request that the
     *     parser open a regular URI connection to the system identifier.
     * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
     * @throws java.io.IOException A Java-specific IO exception, possibly the result of creating a
     *     new InputStream or Reader for the InputSource.
     * @see org.xml.sax.InputSource
     */
    public InputSource resolveEntity(String publicId, String systemId)
        throws SAXException, IOException {
      if (systemId == null) return null;

      ClassLoader cl = Thread.currentThread().getContextClassLoader();
      cl = cl == null ? ClassLoader.getSystemClassLoader() : cl;

      if (systemId.equals("http://jpos.org/dtd/generic-packager-1.0.dtd")) {
        final URL resource = cl.getResource("org/jpos/iso/packager/genericpackager.dtd");
        return new InputSource(resource.toExternalForm());
      } else if (systemId.equals("http://jpos.org/dtd/generic-validating-packager-1.0.dtd")) {
        final URL resource =
            cl.getResource("org/jpos/iso/packager/generic-validating-packager.dtd");
        return new InputSource(resource.toExternalForm());
      }

      URL url = new URL(systemId);
      if (url.getProtocol().equals("file")) {
        String file = url.getFile();
        if (file.endsWith(".dtd")) {
          File f = new File(file);
          InputStream res = null;
          if (f.exists()) {
            res = new FileInputStream(f);
          }
          if (res == null) {
            String dtdResource = "org/jpos/iso/packager/" + f.getName();
            res = cl.getResourceAsStream(dtdResource);
          }
          if (res != null) return new InputSource(res);
        }
      }
      return null;
    }
 private String getPolitenessKey(URL u) {
   String key = null;
   if (QUEUE_MODE_IP.equalsIgnoreCase(queueMode)) {
     try {
       final InetAddress addr = InetAddress.getByName(u.getHost());
       key = addr.getHostAddress();
     } catch (final UnknownHostException e) {
       // unable to resolve it, so don't fall back to host name
       LOG.warn("Unable to resolve: {}, skipping.", u.getHost());
       return null;
     }
   } else if (QUEUE_MODE_DOMAIN.equalsIgnoreCase(queueMode)) {
     key = PaidLevelDomain.getPLD(u.getHost());
     if (key == null) {
       LOG.warn("Unknown domain for url: {}, using hostname as key", u.toExternalForm());
       key = u.getHost();
     }
   } else {
     key = u.getHost();
     if (key == null) {
       LOG.warn("Unknown host for url: {}, using URL string as key", u.toExternalForm());
       key = u.toExternalForm();
     }
   }
   return key.toLowerCase(Locale.ROOT);
 }
Example #16
0
  private void addRepository(
      URL index,
      Set<URL> visited,
      List<? super Repository> repos,
      int hopCount,
      URLConnector connector,
      File cacheDir)
      throws Exception {
    if (visited.add(index)) {
      CachingURLResourceHandle handle =
          new CachingURLResourceHandle(
              index.toExternalForm(), null, cacheDir, connector, CachingMode.PreferRemote);
      handle.setReporter(Central.getWorkspace());
      File file = handle.request();

      PullParser repoParser = new PullParser();
      RepositoryImpl repo = repoParser.parseRepository(new FileInputStream(file));
      repo.setURI(index.toExternalForm());
      repos.add(repo);

      hopCount--;
      if (hopCount > 0 && repo.getReferrals() != null) {
        for (Referral referral : repo.getReferrals()) {
          URL referralUrl = new URL(index, referral.getUrl());
          hopCount = (referral.getDepth() > hopCount) ? hopCount : referral.getDepth();

          addRepository(referralUrl, visited, repos, hopCount, connector, cacheDir);
        }
      }
    }
  }
 /**
  * Loads settings from a url that represents them using the {@link
  * SettingsLoaderFactory#loaderFromSource(String)}.
  */
 public Builder loadFromUrl(URL url) throws SettingsException {
   try {
     return loadFromStream(url.toExternalForm(), url.openStream());
   } catch (IOException e) {
     throw new SettingsException(
         "Failed to open stream for url [" + url.toExternalForm() + "]", e);
   }
 }
Example #18
0
 /**
  * Returns whether the given URLs are equal - either may be <code>null</code>.
  *
  * @param url1 URL to be compared
  * @param url2 URL to be compared
  * @return whether the given URLs are equal
  */
 protected boolean equalURLs(URL url1, URL url2) {
   if (url1 == null) {
     return url2 == null;
   }
   if (url2 == null) {
     return false;
   }
   return url1.toExternalForm().equals(url2.toExternalForm());
 }
Example #19
0
 @Override
 public boolean exec(MethodContext methodContext) throws MiniLangException {
   List<Object> messages = errorListFma.get(methodContext.getEnvMap());
   if (messages == null) {
     messages = new LinkedList<Object>();
     errorListFma.put(methodContext.getEnvMap(), messages);
   }
   String location = this.locationFse.expandString(methodContext.getEnvMap());
   Delegator delegator = getDelegator(methodContext);
   URL dataUrl = null;
   try {
     dataUrl = FlexibleLocation.resolveLocation(location, methodContext.getLoader());
   } catch (MalformedURLException e) {
     messages.add(
         "Could not find Entity Data document in resource: "
             + location
             + "; error was: "
             + e.toString());
   }
   if (dataUrl == null) {
     messages.add("Could not find Entity Data document in resource: " + location);
   }
   if ("assert".equals(mode)) {
     try {
       EntityDataAssert.assertData(dataUrl, delegator, messages);
     } catch (Exception e) {
       String xmlError =
           "Error checking/asserting XML Resource \""
               + dataUrl.toExternalForm()
               + "\"; Error was: "
               + e.getMessage();
       messages.add(xmlError);
       Debug.logWarning(e, xmlError, module);
     }
   } else {
     try {
       EntitySaxReader reader = null;
       if (timeout > 0) {
         reader = new EntitySaxReader(delegator, timeout);
       } else {
         reader = new EntitySaxReader(delegator);
       }
       reader.parse(dataUrl);
     } catch (Exception e) {
       String xmlError =
           "Error loading XML Resource \""
               + dataUrl.toExternalForm()
               + "\"; Error was: "
               + e.getMessage();
       messages.add(xmlError);
       Debug.logWarning(e, xmlError, module);
     }
   }
   return true;
 }
  private String source(URL url) {
    File baseDirectory = dataDir().getResourceLoader().getBaseDirectory();

    if (url.getProtocol().equals("file")) {
      File file = Files.url(baseDirectory, url.toExternalForm());
      if (file != null && !file.isAbsolute()) {
        return Paths.convert(baseDirectory, file);
      }
    }
    return url.toExternalForm();
  }
Example #21
0
 @Nullable
 public static String getCssReference(@Nonnull final String baseName) {
   @Nullable final URL bss = Util.class.getResource(baseName + ".bss");
   if (bss != null) {
     return bss.toExternalForm();
   }
   @Nullable final URL css = Util.class.getResource(baseName + ".css");
   if (css != null) {
     return css.toExternalForm();
   }
   return null;
 }
 /**
  * Renvoie l'url du r?pertoire parent du fichier ou r?pertoire correspondant ? l'URL donn?e, ou
  * null si l'on ne peut pas d?terminer le r?pertoire parent.
  */
 protected static URL getURLParent(final URL u) {
   final int index = u.toExternalForm().lastIndexOf("/");
   if (index >= 0) {
     try {
       return (new URL(u.toExternalForm().substring(0, index)));
     } catch (final MalformedURLException ex) {
       LOG.error("getURLParent(" + u + ") : MalformedURLException", ex);
       return (null);
     }
   }
   return (null);
 }
 private void updateLafDarcula() {
   Platform.runLater(
       () -> {
         final URL engineStyleUrl = getClass().getResource("/style/javaFXBrowserDarcula.css");
         final URL scrollBarStyleUrl =
             getClass().getResource("/style/javaFXBrowserDarculaScrollBar.css");
         myEngine.setUserStyleSheetLocation(engineStyleUrl.toExternalForm());
         myPane.getStylesheets().add(scrollBarStyleUrl.toExternalForm());
         myPane.setStyle("-fx-background-color: #3c3f41");
         myPanel.getScene().getStylesheets().add(engineStyleUrl.toExternalForm());
         myEngine.reload();
       });
 }
Example #24
0
  private void compareQuery(URL htmlURL, URL compareURL) throws SAXException, IOException {
    Query query = QueryFactory.read(compareURL.toExternalForm());

    Map<String, Query> qs =
        QueryUtilities.makeQueries(ParserFactory.Format.XHTML, htmlURL.toExternalForm());

    assertTrue("We have a query", qs.size() != 0);

    Query qFromHTML = qs.get(qs.keySet().toArray()[0]);

    assertEquals(
        "Query matches (" + htmlURL + ")", Algebra.compile(query), Algebra.compile(qFromHTML));
  }
Example #25
0
 /**
  * Guess url.
  *
  * @param baseURL the base url
  * @param spec the spec
  * @return the url
  * @throws MalformedURLException the malformed url exception
  */
 public static URL guessURL(URL baseURL, String spec) throws MalformedURLException {
   URL finalURL = null;
   try {
     if (baseURL != null) {
       int colonIdx = spec.indexOf(':');
       String newProtocol = colonIdx == -1 ? null : spec.substring(0, colonIdx);
       if ((newProtocol != null) && !newProtocol.equalsIgnoreCase(baseURL.getProtocol())) {
         baseURL = null;
       }
     }
     finalURL = createURL(baseURL, spec);
   } catch (MalformedURLException | UnsupportedEncodingException mfu) {
     spec = spec.trim();
     int idx = spec.indexOf(':');
     if (idx == -1) {
       int slashIdx = spec.indexOf('/');
       if (slashIdx == 0) {
         // A file, absolute
         finalURL = new URL("file:" + spec);
       } else {
         if (slashIdx == -1) {
           // No slash, no colon, must be host.
           finalURL = new URL(baseURL, "http://" + spec);
         } else {
           String possibleHost = spec.substring(0, slashIdx).toLowerCase();
           if (Domains.isLikelyHostName(possibleHost)) {
             finalURL = new URL(baseURL, "http://" + spec);
           } else {
             finalURL = new URL(baseURL, "file:" + spec);
           }
         }
       }
     } else {
       if (idx == 1) {
         // Likely a drive
         finalURL = new URL(baseURL, "file:" + spec);
       } else {
         try {
           throw mfu;
         } catch (IOException e) {
           // TODO Auto-generated catch block
           logger.severe(e.getMessage());
         }
       }
     }
   }
   if (!"".equals(finalURL.getHost()) && (finalURL.toExternalForm().indexOf(' ') != -1)) {
     throw new MalformedURLException("There are blanks in the URL: " + finalURL.toExternalForm());
   }
   return finalURL;
 }
 @DataBoundConstructor
 public RedmineSite(String name, URL url, String apiAccessKey, String projectId) {
   if (!url.toExternalForm().endsWith("/")) {
     try {
       url = new URL(url.toExternalForm() + "/");
     } catch (MalformedURLException e) {
       throw new AssertionError(e); // impossible
     }
   }
   this.name = name;
   this.url = url;
   this.apiAccessKey = apiAccessKey;
   this.projectId = projectId;
 }
  @Override
  protected void informResponse(
      TRTrackerAnnouncerHelper helper, TRTrackerAnnouncerResponse response) {
    URL url = response.getURL();

    // can be null for external plugins (e.g. mldht...)

    if (url != null) {

      synchronized (this) {
        String key = url.toExternalForm();

        StatusSummary summary = recent_responses.get(key);

        if (summary != null) {

          summary.updateFrom(response);
        }
      }
    }

    last_response_informed = response;

    // force recalc of best active next time

    last_best_active_set_time = 0;

    super.informResponse(helper, response);

    if (response.getStatus() != TRTrackerAnnouncerResponse.ST_ONLINE) {

      URL u = response.getURL();

      if (u != null) {

        String s = u.toExternalForm();

        synchronized (failed_urls) {
          if (failed_urls.contains(s)) {

            return;
          }

          failed_urls.add(s);
        }
      }

      checkActivation(true);
    }
  }
  private static File computeApplicationDir(URL location, File defaultDir) {
    if (location == null) {
      System.out.println("Warning: Cannot locate the program directory. Assuming default.");
      return defaultDir;
    }
    if (!"file".equalsIgnoreCase(location.getProtocol())) {
      System.out.println("Warning: Unrecognized location type. Assuming default.");
      return new File(".");
    }
    String file = location.getFile();
    if (!file.endsWith(".jar") && !file.endsWith(".zip")) {
      try {
        return (new File(URLDecoder.decode(location.getFile(), "UTF-8"))).getParentFile();
      } catch (UnsupportedEncodingException e) {

      }

      System.out.println("Warning: Unrecognized location type. Assuming default.");
      return new File(location.getFile());
    } else {

      try {
        File path = null; // new File(URLDecoder.decode(location.toExternalForm().substring(6),
        // "UTF-8")).getParentFile();
        if (!CommonLauncher.isLinux() && !CommonLauncher.isOSX()) {
          path =
              new File(URLDecoder.decode(location.toExternalForm().substring(6), "UTF-8"))
                  .getParentFile();
        } else {
          path =
              new File(URLDecoder.decode(location.toExternalForm().substring(5), "UTF-8"))
                  .getParentFile();
        }
        //                System.out.println("path: " + path.getAbsolutePath());
        //                System.out.println("location: " + location.getPath());
        //                System.out.println("external from location: " +
        // URLDecoder.decode(location.toExternalForm().substring(6), "UTF-8"));
        //                System.out.println("external from location + 6: " +
        // URLDecoder.decode(location.toExternalForm(), "UTF-8"));

        return path;
      } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    System.out.println("Warning: Unrecognized location type. Assuming default.");
    return new File(location.getFile());
  }
Example #29
0
  private void evalUrlAndPop(URL urlIn) throws SoarException {
    final URL url = normalizeUrl(urlIn);

    try {
      fileStack.push(url.toExternalForm());
      if (topLevelState != null) {
        topLevelState.files.add(new FileInfo(url.toExternalForm()));
      }
      interp.eval(url);
    } finally {
      fileStack.pop();
      popd();
    }
  }
Example #30
0
 private void setDefaultUrl(URL selectedURL) {
   if (selectedURL != null) {
     comboUrl.setText(selectedURL.toExternalForm());
     url = selectedURL.toExternalForm();
     setPageComplete(true);
   } else if (url != null && url.length() != 0) {
     comboUrl.setText(url);
     setPageComplete(true);
   } else {
     url = null;
     comboUrl.setText("http://"); // $NON-NLS-1$
     setPageComplete(false);
   }
 }