Ejemplo n.º 1
0
  public static ImageFormat getImageFormat(String rawImageLink, String imageNamespace) {
    ImageFormat img = new ImageFormat();
    List<String> list = WikipediaScanner.splitByPipe(rawImageLink, null);
    if (list.size() > 0) {
      String attrValue;
      String token = list.get(0);
      img.setFilename("");
      if (token.length() > imageNamespace.length()
          && token.charAt(imageNamespace.length()) == ':') {
        if (imageNamespace.equalsIgnoreCase(token.substring(0, imageNamespace.length()))) {
          img.setFilename(token.substring(imageNamespace.length() + 1));
          img.setNamespace(imageNamespace);
        }
      }
      String caption;
      for (int j = 1; j < list.size(); j++) {
        caption = list.get(j).trim();
        if (caption.length() > 0) {
          token = caption.toLowerCase();
          int defIndex = token.indexOf("=");
          if (defIndex > 0) {
            token = token.substring(0, defIndex).trim();
            if (token.equals("link")) {
              attrValue = caption.substring(defIndex + 1).trim();
              img.setLink(attrValue);
              continue;
            }
            if (token.equals("alt")) {
              attrValue = caption.substring(defIndex + 1).trim();
              img.setAlt(Encoder.encodeHtml(attrValue));
              continue;
            }
          } else {
            if (token.equals("frame")
                || token.equals("thumb")
                || token.equals("thumbnail")
                || token.equals("border")) {
              img.setType(token);
              continue;
            }

            if (token.equals("right")
                || token.equals("left")
                || token.equals("center")
                || token.equals("none")) {
              img.setLocation(token);
              continue;
            }

            if (token.endsWith("px")) {
              img.setSize(token);
              continue;
            }
          }
          img.setCaption(caption);
        }
      }
    }
    return img;
  }
Ejemplo n.º 2
0
  /**
   * Perform external link processing.
   *
   * <p>
   *
   * <p>According to {@link <a
   * href="http://download.oracle.com/otn-pub/jcp/jaxrs-2_0-fr-eval-spec/jsr339-jaxrs-2.0-final-spec.pdf">
   * JAX-RS specification section 3.7.3</a>}: "4. If the resulting string ends with ‘/’ then remove
   * the final character". To differentiate links the trailing slash is checked manually.
   */
  @GET
  @Path("link/{spec:(Http|Https|Ftp|Smb|Sftp|Scp)://.*}")
  @Produces("text/html")
  public Redirect externalLink(
      @PathParam("spec") String spec,
      @Context HttpServletRequest req,
      @Context HttpServletResponse resp)
      throws Exception {

    if (req.getRequestURI().endsWith("/") && !spec.endsWith("/")) {
      spec += "/";
    }

    Matcher matcher = EXT_LINK_REGEXP.matcher(spec);
    if (!matcher.matches()) {
      throw new BadRequestException();
    }

    URL url;
    try {
      url = new URL(spec);
    } catch (MalformedURLException ignored) {
      throw new BadRequestException();
    }

    Matcher cematcher = CHECK_ENCODED_REGEXP.matcher(url.getPath());
    String path = cematcher.matches() ? Encoder.encodeUrl(url.getPath()) : url.getPath();

    return new Redirect(
        new URL(
                url.getProtocol(),
                IDN.toASCII(url.getHost()),
                url.getPort(),
                path + (!StringUtils.isBlank(url.getQuery()) ? "?" + url.getQuery() : ""))
            .toURI());
  }