Example usage for com.google.common.net MediaType toString

List of usage examples for com.google.common.net MediaType toString

Introduction

In this page you can find the example usage for com.google.common.net MediaType toString.

Prototype

String toString

To view the source code for com.google.common.net MediaType toString.

Click Source Link

Usage

From source file:com.facebook.buck.httpserver.Responses.java

/**
 * Writes the specified content to the response with the specified status code.
 *//*from  w w w .  j  a  va2 s. c  om*/
private static void writeResponse(String content, MediaType mediaType, Request baseRequest,
        HttpServletResponse response, int status) throws IOException {
    response.setContentType(mediaType.toString());
    response.setStatus(status);

    response.getWriter().write(content);

    response.flushBuffer();
    baseRequest.setHandled(true);
}

From source file:google.registry.util.UrlFetchUtils.java

/**
 * Sets payload on request as a {@code multipart/form-data} request.
 *
 * <p>This is equivalent to running the command: {@code curl -F fieldName=@payload.txt URL}
 *
 * @see <a href="http://www.ietf.org/rfc/rfc2388.txt"> RFC2388 - Returning Values from Forms</a>
 *//*www .  j  a v  a2s. c o m*/
public static void setPayloadMultipart(HTTPRequest request, String name, String filename, MediaType contentType,
        String data) {
    String boundary = createMultipartBoundary();
    checkState(!data.contains(boundary), "Multipart data contains autogenerated boundary: %s", boundary);
    StringBuilder multipart = new StringBuilder();
    multipart.append(format("--%s\r\n", boundary));
    multipart.append(
            format("%s: form-data; name=\"%s\"; filename=\"%s\"\r\n", CONTENT_DISPOSITION, name, filename));
    multipart.append(format("%s: %s\r\n", CONTENT_TYPE, contentType.toString()));
    multipart.append("\r\n");
    multipart.append(data);
    multipart.append("\r\n");
    multipart.append(format("--%s--", boundary));
    byte[] payload = multipart.toString().getBytes(UTF_8);
    request.addHeader(new HTTPHeader(CONTENT_TYPE, format("multipart/form-data; boundary=\"%s\"", boundary)));
    request.addHeader(new HTTPHeader(CONTENT_LENGTH, Integer.toString(payload.length)));
    request.setPayload(payload);
}

From source file:lu.list.itis.dkd.assess.cloze.util.UrlHelper.java

/**
 * This method requires an proper UTF8 encoded url to connect to a website and returns
 * the source code of the page or an empty String if the connection failed.
 * @param encodedUrl//from  w  w w. ja v a 2 s  . c  om
 * @param A mediatype. E.g. JSON_UTF_8 or MediaType.APPLICATION_XML_UTF_8
 * @return
 */
public static String getSource(String encodedUrl, MediaType mediatype) {
    URL myURL;
    try {
        //Connect to url
        myURL = new URL(encodedUrl);
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

        HttpURLConnection myURLConnection = (HttpURLConnection) myURL.openConnection();

        //Set mediatype
        myURLConnection.setRequestProperty(HttpHeaders.ACCEPT.toString(), mediatype.toString());
        myURLConnection.connect();

        int status = myURLConnection.getResponseCode();
        if (status == 500) {
            return "";
        }

        final InputStream is = myURLConnection.getInputStream();
        final BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF8"));
        final StringBuilder sb = new StringBuilder();

        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
        }

        return sb.toString();
    } catch (IOException e) {
        //TODO Stop when ...
        //https://cloud.google.com/storage/docs/exponential-backoff            
        logger.log(Level.INFO, encodedUrl + " is busy. Waiting 1 minute and retry.");
        try {
            Thread.sleep(64000);
            getSource(encodedUrl);
        } catch (InterruptedException e1) {
            logger.log(Level.SEVERE, "Connection to " + encodedUrl + " failed permanently.");
            return "";
        }
        return "";
    }
}

From source file:com.mastfrog.acteur.headers.MediaTypeHeader.java

@Override
public String toString(MediaType value) {
    return value.toString();
}

From source file:com.kolich.curacao.entities.mediatype.AbstractContentTypeCuracaoEntity.java

public AbstractContentTypeCuracaoEntity(final int statusCode, final MediaType mediaType) {
    this(statusCode, mediaType.toString());
}

From source file:google.registry.request.ResponseImpl.java

@Override
public void setContentType(MediaType contentType) {
    rsp.setContentType(contentType.toString());
}

From source file:com.bennavetta.appsite.serve.HttpServletResp.java

@Override
public void setContentType(MediaType type) {
    response.setContentType(type.toString());
}

From source file:com.bennavetta.appsite.processor.GroovyProcessor.java

@Override
public boolean canProcess(String path, MediaType type, Request request, Response response) {
    if (path.endsWith(".groovy"))
        return true;
    if (type.toString().contains("groovy") && type.toString().contains("template")) // don't process groovy templates
        return true;
    return false;
}

From source file:google.registry.storage.drive.DriveConnection.java

/** Constructs an object representing a file (or folder) with a given title and parent. */
private File createFileReference(String title, MediaType mimeType, @Nullable String parentFolderId) {
    return new File().setTitle(title).setMimeType(mimeType.toString()).setParents(
            parentFolderId == null ? null : ImmutableList.of(new ParentReference().setId(parentFolderId)));
}

From source file:org.jclouds.googlecloudstorage.domain.templates.ObjectTemplate.java

public ObjectTemplate contentType(MediaType contentType) {
    this.contentType = contentType.toString();
    return this;
}