public UrlList getUrlList(String url) {
   UrlList urlList = null;
   try {
     String key = Hash.hashKey(url);
     GetObjectRequest req = new GetObjectRequest(bucketName, key);
     S3Object s3Object = s3client.getObject(req);
     InputStream objectData = s3Object.getObjectContent();
     BufferedReader reader = new BufferedReader(new InputStreamReader(objectData));
     StringBuilder s3Content = new StringBuilder();
     String line;
     while ((line = reader.readLine()) != null) {
       s3Content.append(line + "\r\n");
     }
     reader.close();
     objectData.close();
     ObjectMapper mapper = new ObjectMapper();
     mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
     mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
     urlList = mapper.readValue(s3Content.toString(), UrlList.class);
   } catch (AmazonS3Exception ase) {
     System.out.println("S3UrlListDA : document does not exist");
     ase.printStackTrace();
   } catch (IOException e) {
     System.out.println("S3UrlListDA : IOException while fetching document from S3");
     e.printStackTrace();
   } catch (NoSuchAlgorithmException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return urlList;
 }
 /**
  * Initialize object mapper.
  *
  * @return the object mapper
  */
 protected ObjectMapper initializeObjectMapper() {
   final ObjectMapper mapper = new ObjectMapper(getJsonFactory());
   mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
   mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
   mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC);
   mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC);
   mapper.setVisibility(
       PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC);
   mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
   return mapper;
 }
  private ObjectMapper buildObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();

    // serialize fields instead of properties
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

    // ignore unresolved fields (mostly 'principal')
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    objectMapper.setPropertyNamingStrategy(new MongoIdNamingStrategy());
    return objectMapper;
  }
  public void putUrlList(UrlList urlList) {
    try {
      // hash url to get key
      String s3Key = Hash.hashKey(urlList.getParentUrl());

      // put document into s3
      ObjectMapper mapper = new ObjectMapper();
      mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
      mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
      ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
      String json = ow.writeValueAsString(urlList);
      ByteArrayInputStream inputStream = new ByteArrayInputStream(json.getBytes());
      ObjectMetadata omd = new ObjectMetadata();
      omd.setContentLength(json.getBytes().length);
      PutObjectRequest request = new PutObjectRequest(bucketName, s3Key, inputStream, omd);
      s3client.putObject(request);

      // No need to put into dynamo as it should already be there

      /*
      ObjectMapper mapper = new ObjectMapper();
      mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
      mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
      ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
      String json = ow.writeValueAsString(doc);
      ObjectMetadata omd = new ObjectMetadata();
      omd.setContentType("text/html");
      omd.setContentLength(json.length());
      ByteArrayInputStream inputStream = new ByteArrayInputStream(
      		json.getBytes());
      PutObjectRequest request = new PutObjectRequest(bucketName, s3Key,
      		inputStream, omd);
      s3client.putObject(request);
      dynamo.putItem("documentUrl", doc.getDocumentId(), "s3Key", s3Key);*/
    } catch (JsonProcessingException e) {
      System.out.println("S3UrlListDA : json processing excecption exception");
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  @Test
  public final void
      givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException()
          throws JsonParseException, IOException {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, containsString("stringValue"));
    assertThat(dtoAsString, containsString("booleanValue"));
  }
  /** Related to issues [JACKSON-155], [#170]. */
  public void testFile() throws Exception {
    // Not portable etc... has to do:
    File src = new File("/test").getAbsoluteFile();
    String abs = src.getAbsolutePath();

    // escape backslashes (for portability with windows)
    String json = MAPPER.writeValueAsString(abs);
    File result = MAPPER.readValue(json, File.class);
    assertEquals(abs, result.getAbsolutePath());

    // Then #170
    final ObjectMapper mapper2 = new ObjectMapper();
    mapper2.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);

    result = mapper2.readValue(json, File.class);
    assertEquals(abs, result.getAbsolutePath());
  }
Exemple #7
0
  public void testJackson() throws IOException {
    Message message = new Message();
    message.setFrom("wangtao");
    message.setTo("wangtao2");
    message.setPacketID(Packet.nextID());
    message.setBody("bodyTEst");
    message.setType("nonmail");
    message.setError(new com.tao.realweb.bean.PacketError(Condition.bad_request));
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    String jsonStr = mapper.writeValueAsString(message);
    System.out.println(jsonStr);

    Message map = mapper.readValue(jsonStr, Message.class);

    System.out.println(map.getPacketID());
    System.out.println(map.getFrom());
    System.out.println(map.getTo());
    System.out.println(map.getType());
    System.out.println(map.getError().toString());
    System.out.println(map.getBody());
  }
Exemple #8
0
  public static void post(String idProjeto, MensagemJson conteudo) {
    try {
      URL url = new URL("https://android.googleapis.com/gcm/send");
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "application/json");
      conn.setRequestProperty("Authorization", "key=" + idProjeto);
      conn.setDoOutput(true);

      ObjectMapper mapeador = new ObjectMapper();
      mapeador.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
      DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
      mapeador.writeValue(wr, conteudo);
      wr.flush();
      wr.close();

      int respostaHttp = conn.getResponseCode();
      System.out.println("\nEnviando HTTP POST para: " + url);
      System.out.println("Código HTTP: " + respostaHttp);

      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();

      while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
      }
      in.close();

      System.out.println(response.toString());
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 @Before
 public void setUp() {
   mapper = new ObjectMapper();
   mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
   mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
 }
 public FlattenJSONBenchmarkUtil() {
   this.rng = new Random(9999);
   mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.PUBLIC_ONLY);
   mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
 }