com.google.caja.lexer.FetchedData

Here are the examples of the java api class com.google.caja.lexer.FetchedData taken from open source projects.

1. ProxyServlet#handle()

Project: caja
File: ProxyServlet.java
/**
   * Main entry point for the fetching proxy.
   *
   * @param args a set of arguments to the cajoling service.
   * @param mq a message queue into which status and error messages will be
   *     placed. The caller should query for the most severe status of the
   *     messages in this queue to determine the overall success of the
   *     invocation.
   * @return the output content, or {@code null} if a serious error occurred
   *     that prevented the content from being generated.
   */
public FetchedData handle(ContentHandlerArgs args, MessageQueue mq) {
    FetchedData result = doHandle(args, mq);
    if (result == null) {
        ByteArrayOutputStream intermediateResponse = new ByteArrayOutputStream();
        Pair<ContentType, String> contentParams = getReturnedContentParams(args);
        OutputStreamWriter writer = new OutputStreamWriter(intermediateResponse, Charsets.UTF_8);
        try {
            renderAsJSON((String) null, (String) null, contentParams.b, mq, writer, false);
        } catch (IOException e) {
            throw new SomethingWidgyHappenedError(e);
        }
        result = FetchedData.fromBytes(intermediateResponse.toByteArray(), contentParams.a.mimeType, "UTF-8", InputSource.UNKNOWN);
    }
    return result;
}

2. ProxyServlet#doHandle()

Project: caja
File: ProxyServlet.java
private FetchedData doHandle(ContentHandlerArgs args, MessageQueue mq) {
    String inputUrlString = CajaArguments.URL.get(args);
    URI inputUri;
    if (inputUrlString == null) {
        mq.addMessage(ServiceMessageType.MISSING_ARGUMENT, MessagePart.Factory.valueOf(CajaArguments.URL.toString()));
        return null;
    } else {
        try {
            inputUri = new URI(inputUrlString);
        } catch (URISyntaxException ex) {
            mq.addMessage(ServiceMessageType.INVALID_INPUT_URL, MessagePart.Factory.valueOf(inputUrlString));
            return null;
        }
    }
    String expectedInputContentType = CajaArguments.INPUT_MIME_TYPE.get(args);
    if (expectedInputContentType == null) {
        mq.addMessage(ServiceMessageType.MISSING_ARGUMENT, MessagePart.Factory.valueOf(CajaArguments.INPUT_MIME_TYPE.toString()));
        return null;
    }
    FetchedData inputFetchedData;
    try {
        inputFetchedData = uriFetcher.fetch(new ExternalReference(inputUri, FilePosition.UNKNOWN), expectedInputContentType);
    } catch (UriFetcher.UriFetchException ex) {
        ex.toMessageQueue(mq);
        return null;
    }
    if (!typeCheck.check(expectedInputContentType, inputFetchedData.getContentType())) {
        mq.addMessage(ServiceMessageType.UNEXPECTED_INPUT_MIME_TYPE, MessagePart.Factory.valueOf(expectedInputContentType), MessagePart.Factory.valueOf(inputFetchedData.getContentType()));
        return null;
    }
    ByteArrayOutputStream intermediateResponse = new ByteArrayOutputStream();
    Pair<String, String> contentInfo;
    try {
        contentInfo = applyHandler(inputUri, args, inputFetchedData, intermediateResponse, mq);
    } catch (UnsupportedContentTypeException e) {
        mq.addMessage(ServiceMessageType.UNSUPPORTED_CONTENT_TYPES);
        return null;
    } catch (RuntimeException e) {
        mq.addMessage(ServiceMessageType.EXCEPTION_IN_SERVICE, MessagePart.Factory.valueOf(e.toString()));
        return null;
    }
    return FetchedData.fromBytes(intermediateResponse.toByteArray(), contentInfo.a, contentInfo.b, new InputSource(inputUri));
}

3. ProxyServlet#doGet()

Project: caja
File: ProxyServlet.java
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
    ContentHandlerArgs args = new HttpContentHandlerArgs(req);
    // URL path parameters can trick IE into misinterpreting responses as HTML
    if (req.getRequestURI().contains(";")) {
        throw new ServletException("Invalid URL path parameter");
    }
    MessageQueue mq = new SimpleMessageQueue();
    FetchedData result = handle(args, mq);
    if (result == null) {
        closeBadRequest(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, mq);
        return;
    }
    resp.setStatus(HttpServletResponse.SC_OK);
    String responseContentType = result.getContentType();
    if (result.getCharSet() != null) {
        responseContentType += ";charset=" + result.getCharSet();
    }
    if (containsNewline(responseContentType)) {
        throw new IllegalArgumentException(responseContentType);
    }
    try {
        byte[] content = result.getByteContent();
        resp.setContentType(responseContentType);
        resp.setContentLength(content.length);
        resp.setHeader(UMP.a, UMP.b);
        resp.setHeader("X-Content-Type-Options", "nosniff");
        resp.setHeader("Content-Disposition", "attachment; filename=f.txt");
        resp.getOutputStream().write(content);
        resp.getOutputStream().close();
    } catch (IOException ex) {
        throw (ServletException) new ServletException().initCause(ex);
    }
}