Java Code Examples for com.google.api.client.json.JsonGenerator#flush()

The following examples show how to use com.google.api.client.json.JsonGenerator#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnauthorizedServlet.java    From HotswapAgentExamples with GNU General Public License v2.0 6 votes vote down vote up
protected void unauthorized(HttpServletRequest req, HttpServletResponse resp)
		throws IOException {

	String userAgent = req.getHeader("User-Agent");

	resp.setHeader("Content-Type", "text/javascript");
	resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

	JsonGenerator generator = jsonFactory.createJsonGenerator(resp.getWriter());
	generator.writeStartObject();
	generator.writeFieldName("error");
	generator.writeString("Login required.");
	generator.writeFieldName("q");
	generator.writeString(req.getQueryString());
	generator.writeFieldName("agent");
	generator.writeString(userAgent);
	generator.writeEndObject();
	generator.flush();
}
 
Example 2
Source File: JsonHttpContent.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void writeTo(OutputStream out) throws IOException {
  JsonGenerator generator = jsonFactory.createJsonGenerator(out, getCharset());
  if (wrapperKey != null) {
    generator.writeStartObject();
    generator.writeFieldName(wrapperKey);
  }
  generator.serialize(data);
  if (wrapperKey != null) {
    generator.writeEndObject();
  }
  generator.flush();
}
 
Example 3
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public final void testGenerateEntry() throws Exception {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  JsonGenerator generator = newFactory().createJsonGenerator(out, Charsets.UTF_8);
  Entry entry = new Entry();
  entry.title = "foo";
  generator.serialize(entry);
  generator.flush();
  assertEquals(JSON_ENTRY, StringUtils.newStringUtf8(out.toByteArray()));
}
 
Example 4
Source File: AbstractJsonFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public final void testGenerateFeed() throws Exception {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  JsonGenerator generator = newFactory().createJsonGenerator(out, Charsets.UTF_8);
  Feed feed = new Feed();
  Entry entryFoo = new Entry();
  entryFoo.title = "foo";
  Entry entryBar = new Entry();
  entryBar.title = "bar";
  feed.entries = new ArrayList<Entry>();
  feed.entries.add(entryFoo);
  feed.entries.add(entryBar);
  generator.serialize(feed);
  generator.flush();
  assertEquals(JSON_FEED, StringUtils.newStringUtf8(out.toByteArray()));
}
 
Example 5
Source File: RemoteGoogleDriveConnector.java    From cloudsync with GNU General Public License v2.0 5 votes vote down vote up
private void storeClientToken(final JsonFactory jsonFactory) throws IOException
{
	final StringWriter jsonTrWriter = new StringWriter();
	final JsonGenerator generator = jsonFactory.createJsonGenerator(jsonTrWriter);
	generator.serialize(clientToken);
	generator.flush();
	generator.close();

	FileUtils.writeStringToFile(clientTokenPath.toFile(), jsonTrWriter.toString(), charset);
}
 
Example 6
Source File: ConfigDataStoreFactory.java    From googleads-shopping-samples with Apache License 2.0 5 votes vote down vote up
private void writeToken() throws IOException {
  try (OutputStream outputStream = new FileOutputStream(tokenFile);
      OutputStreamWriter outputWriter = new OutputStreamWriter(outputStream)) {
    JsonGenerator generator = new JacksonFactory().createJsonGenerator(outputWriter);
    generator.enablePrettyPrint();
    generator.serialize(token);
    generator.flush();
  }
}