public static PackageAttributes forBytesToStage( byte[] bytes, String targetName, String stagingPath) { Hasher hasher = Hashing.md5().newHasher(); String hash = Base64Variants.MODIFIED_FOR_URL.encode(hasher.putBytes(bytes).hash().asBytes()); long size = bytes.length; String uniqueName = getUniqueContentName(new File(targetName), hash); String resourcePath = FileSystems.matchNewResource(stagingPath, true) .resolve(uniqueName, StandardResolveOptions.RESOLVE_FILE) .toString(); DataflowPackage target = new DataflowPackage(); target.setName(uniqueName); target.setLocation(resourcePath); return new AutoValue_PackageUtil_PackageAttributes(null, bytes, target, size, hash); }
public final String deserialize(JsonParser paramJsonParser, DeserializationContext paramDeserializationContext) { String str = paramJsonParser.getValueAsString(); if (str != null) return str; JsonToken localJsonToken = paramJsonParser.getCurrentToken(); if (localJsonToken == JsonToken.VALUE_EMBEDDED_OBJECT) { Object localObject = paramJsonParser.getEmbeddedObject(); if (localObject == null) return null; if ((localObject instanceof byte[])) return Base64Variants.getDefaultVariant().encode((byte[])localObject, false); return localObject.toString(); } throw paramDeserializationContext.mappingException(this._valueClass, localJsonToken); }
private static ObjectMapper prettyMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setBase64Variant(Base64Variants.MODIFIED_FOR_URL); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); mapper.registerModule(tuuidModule()); if (!Env.dev()) { mapper.registerModule(new AfterburnerModule()); } DefaultPrettyPrinter pp = new DefaultPrettyPrinter(); pp = pp.withObjectIndenter(new DefaultIndenter(" ", "\n")); mapper.setDefaultPrettyPrinter(pp); return mapper; }
public void testBase64Variant() throws Exception { Base64Variant orig = Base64Variants.PEM; byte[] stuff = jdkSerialize(orig); Base64Variant back = jdkDeserialize(stuff); assertSame(orig, back); }
@Override public byte[] fromString(String string) { if ( string == null ) { return null; } return Base64Variants.MIME_NO_LINEFEEDS.decode( string ); }
public CustomObjectMapper() { //使用默认时区 BaseSettings baseSettings = new BaseSettings(new BasicClassIntrospector(), DEFAULT_ANNOTATION_INTROSPECTOR, STD_VISIBILITY_CHECKER, null, TypeFactory.defaultInstance(), null, null, null, Locale.getDefault(), TimeZone.getDefault(), Base64Variants.getDefaultVariant() ); _serializationConfig = new SerializationConfig(baseSettings, _subtypeResolver, _mixInAnnotations); _deserializationConfig = new DeserializationConfig(baseSettings, _subtypeResolver, _mixInAnnotations); configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, true); // 序列化枚举时的处理 SimpleModule serializerModule = new SimpleModule("enumSerializer"); serializerModule.addSerializer(Enum.class, new EnumSerializer()); registerModule(serializerModule); // 反序列化枚举时的处理 SimpleModule deserializerModule = new SimpleModule("enumDeserializer"); deserializerModule.addDeserializer(Enum.class, new EnumDeserializer()); registerModule(deserializerModule); //日期的处理 //默认是将日期类型转换为yyyy-MM-dd HH:mm //如果需要自定义的,则在pojo对象的Date类型上加上注解 //@com.fasterxml.jackson.annotation.JsonFormat(pattern = "时间格式化") setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm")); configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true); configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true); configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true); configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); }
/** * Creates an Endpoints standard object mapper that allows unquoted field names and unknown * properties. * * Note on unknown properties: When Apiary FE supports a strict mode where properties * are checked against the schema, BE can just ignore unknown properties. This way, FE does * not need to filter out everything that the BE doesn't understand. Before that's done, * a property name with a typo in it, for example, will just be ignored by the BE. */ public static ObjectMapper createStandardObjectMapper(ApiSerializationConfig config) { ObjectMapper objectMapper = new ObjectMapper() .configure(JsonParser.Feature.ALLOW_COMMENTS, true) .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true) .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setBase64Variant(Base64Variants.MODIFIED_FOR_URL) .setSerializerFactory( BeanSerializerFactory.instance.withSerializerModifier(new DeepEmptyCheckingModifier())); AnnotationIntrospector pair = AnnotationIntrospector.pair( new ApiAnnotationIntrospector(config), new JacksonAnnotationIntrospector()); objectMapper.setAnnotationIntrospector(pair); return objectMapper; }
public static PackageAttributes forFileToStage(File source, String stagingPath) throws IOException { // Compute size and hash in one pass over file or directory. long size; String hash; Hasher hasher = Hashing.md5().newHasher(); OutputStream hashStream = Funnels.asOutputStream(hasher); try (CountingOutputStream countingOutputStream = new CountingOutputStream(hashStream)) { if (!source.isDirectory()) { // Files are staged as-is. Files.asByteSource(source).copyTo(countingOutputStream); } else { // Directories are recursively zipped. ZipFiles.zipDirectory(source, countingOutputStream); } countingOutputStream.flush(); size = countingOutputStream.getCount(); hash = Base64Variants.MODIFIED_FOR_URL.encode(hasher.hash().asBytes()); } String uniqueName = getUniqueContentName(source, hash); String resourcePath = FileSystems.matchNewResource(stagingPath, true) .resolve(uniqueName, StandardResolveOptions.RESOLVE_FILE) .toString(); DataflowPackage target = new DataflowPackage(); target.setName(uniqueName); target.setLocation(resourcePath); return new AutoValue_PackageUtil_PackageAttributes(source, null, target, size, hash); }
/** * MailChimp API v3 supports non expires access_token. Then no need refresh_token * * @param task * @return */ private String getAuthorizationHeader(final MailChimpOutputPluginDelegate.PluginTask task) { switch (task.getAuthMethod()) { case OAUTH: return "OAuth " + task.getAccessToken().orNull(); case API_KEY: return "Basic " + Base64Variants.MIME_NO_LINEFEEDS .encode(("apikey" + ":" + task.getApikey().orNull()).getBytes()); default: throw new ConfigException("Not supported method"); } }
private void setupMethod ( HttpRequest request, Map<String, String> headers ) { if (headers != null) { for (String k : headers.keySet()) { request.addHeader(k, headers.get(k)); } } String authorization = "Basic"; String encoded = Base64Variants.MIME_NO_LINEFEEDS.encode((username + ":" + password).getBytes()); authorization = "Basic " + encoded; request.addHeader("Authorization", authorization); }
public static XmlMapper newMapper() { XmlMapper mapper = new XmlMapper(); mapper.setBase64Variant(Base64Variants.MODIFIED_FOR_URL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); if (!Env.dev()) { mapper.registerModule(new AfterburnerModule()); } return mapper; }
public static ObjectMapper newMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setBase64Variant(Base64Variants.MODIFIED_FOR_URL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.registerModule(tuuidModule()); if (!Env.dev()) { mapper.registerModule(new AfterburnerModule()); } return mapper; }
/** * Compute and cache the attributes of a classpath element that we will need to stage it. */ private StagedPackage createStagedPackage(UploadPair uploadPair) { String classpathElement = uploadPair.classpathElement(); String stagingPath = uploadPair.stagingPath(); String overridePackageName = null; if (classpathElement.contains("=")) { String[] components = classpathElement.split("=", 2); overridePackageName = components[0]; classpathElement = components[1]; } File classpathFile = new File(classpathElement); try { Path targetPathPath = Paths.get(URI.create(stagingPath)); boolean directory = classpathFile.isDirectory(); // Compute size and hash in one pass over file or directory. Hasher hasher = Hashing.md5().newHasher(); OutputStream hashStream = Funnels.asOutputStream(hasher); CountingOutputStream countingOutputStream = new CountingOutputStream(hashStream); if (!directory) { // Files are staged as-is. Files.asByteSource(classpathFile).copyTo(countingOutputStream); } else { // Directories are recursively zipped. ZipFiles.zipDirectory(classpathFile, countingOutputStream); } long size = countingOutputStream.getCount(); String hash = Base64Variants.MODIFIED_FOR_URL.encode(hasher.hash().asBytes()); // Create the DataflowPackage with staging name and location. String uniqueName = getUniqueContentName(classpathFile, hash); Path resourcePath = targetPathPath.resolve(uniqueName); return stagedPackage( overridePackageName != null ? overridePackageName : uniqueName, resourcePath.toUri().toString(), size, id); } catch (IOException e) { throw new RuntimeException("Package setup failure for " + classpathElement, e); } }
@Override public String toString(byte[] bytes) { return Base64Variants.MIME_NO_LINEFEEDS.encode( bytes ); }
@Override public ByteBuffer readFromString(String s) { return ByteBuffer.wrap(Base64Variants.getDefaultVariant().decode(s)); }
public final char[] deserialize(JsonParser paramJsonParser, DeserializationContext paramDeserializationContext) { JsonToken localJsonToken1 = paramJsonParser.getCurrentToken(); if (localJsonToken1 == JsonToken.VALUE_STRING) { char[] arrayOfChar1 = paramJsonParser.getTextCharacters(); int i = paramJsonParser.getTextOffset(); int j = paramJsonParser.getTextLength(); char[] arrayOfChar2 = new char[j]; System.arraycopy(arrayOfChar1, i, arrayOfChar2, 0, j); return arrayOfChar2; } if (paramJsonParser.isExpectedStartArrayToken()) { StringBuilder localStringBuilder = new StringBuilder(64); while (true) { JsonToken localJsonToken2 = paramJsonParser.nextToken(); if (localJsonToken2 == JsonToken.END_ARRAY) break; if (localJsonToken2 != JsonToken.VALUE_STRING) throw paramDeserializationContext.mappingException(Character.TYPE); String str = paramJsonParser.getText(); if (str.length() != 1) throw JsonMappingException.from(paramJsonParser, "Can not convert a JSON String of length " + str.length() + " into a char element of char array"); localStringBuilder.append(str.charAt(0)); } return localStringBuilder.toString().toCharArray(); } if (localJsonToken1 == JsonToken.VALUE_EMBEDDED_OBJECT) { Object localObject = paramJsonParser.getEmbeddedObject(); if (localObject == null) return null; if ((localObject instanceof char[])) return (char[])localObject; if ((localObject instanceof String)) return ((String)localObject).toCharArray(); if ((localObject instanceof byte[])) return Base64Variants.getDefaultVariant().encode((byte[])localObject, false).toCharArray(); } throw paramDeserializationContext.mappingException(this._valueClass); }
public final String asText() { return Base64Variants.getDefaultVariant().encode(this._data, false); }
public final String toString() { return Base64Variants.getDefaultVariant().encode(this._data, true); }
public final byte[] binaryValue() { return getBinaryValue(Base64Variants.getDefaultVariant()); }