You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2015/01/01 19:12:09 UTC
[14/32] syncope git commit: [SYNCOPE-620] JPA entities + basic tests
http://git-wip-us.apache.org/repos/asf/syncope/blob/556d5186/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/GuardedStringSerializer.java
----------------------------------------------------------------------
diff --git a/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/GuardedStringSerializer.java b/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/GuardedStringSerializer.java
new file mode 100644
index 0000000..8de59d4
--- /dev/null
+++ b/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/GuardedStringSerializer.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.server.utils.serialization;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import org.identityconnectors.common.Base64;
+import org.identityconnectors.common.security.EncryptorFactory;
+import org.identityconnectors.common.security.GuardedString;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class GuardedStringSerializer extends JsonSerializer<GuardedString> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(GuardedStringSerializer.class);
+
+ @Override
+ public void serialize(final GuardedString source, final JsonGenerator jgen, final SerializerProvider sp)
+ throws IOException, JsonProcessingException {
+
+ jgen.writeStartObject();
+
+ boolean readOnly = false;
+ try {
+ Field field = GuardedString.class.getDeclaredField("readOnly");
+ field.setAccessible(true);
+ readOnly = field.getBoolean(source);
+ } catch (Exception e) {
+ LOG.error("Could not get field value", e);
+ }
+ jgen.writeBooleanField("readOnly", readOnly);
+
+ boolean disposed = false;
+ try {
+ Field field = GuardedString.class.getDeclaredField("disposed");
+ field.setAccessible(true);
+ disposed = field.getBoolean(source);
+ } catch (Exception e) {
+ LOG.error("Could not get field value", e);
+ }
+ jgen.writeBooleanField("disposed", disposed);
+
+ final StringBuilder cleartext = new StringBuilder();
+ ((GuardedString) source).access(new GuardedString.Accessor() {
+
+ @Override
+ public void access(final char[] clearChars) {
+ cleartext.append(clearChars);
+ }
+ });
+ final byte[] encryptedBytes =
+ EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(cleartext.toString().getBytes());
+ jgen.writeStringField("encryptedBytes", Base64.encode(encryptedBytes));
+
+ String base64SHA1Hash = null;
+ try {
+ Field field = GuardedString.class.getDeclaredField("base64SHA1Hash");
+ field.setAccessible(true);
+ base64SHA1Hash = field.get(source).toString();
+ } catch (Exception e) {
+ LOG.error("Could not get field value", e);
+ }
+ if (base64SHA1Hash != null) {
+ jgen.writeStringField("base64SHA1Hash", base64SHA1Hash);
+ }
+
+ jgen.writeEndObject();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/syncope/blob/556d5186/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/POJOHelper.java
----------------------------------------------------------------------
diff --git a/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/POJOHelper.java b/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/POJOHelper.java
new file mode 100644
index 0000000..fb9a888
--- /dev/null
+++ b/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/POJOHelper.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.server.utils.serialization;
+
+import com.fasterxml.jackson.core.Version;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
+import org.identityconnectors.common.security.GuardedString;
+import org.identityconnectors.framework.common.objects.Attribute;
+import org.identityconnectors.framework.common.objects.SyncToken;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Helper class for serialization and deserialization of configuration objects (POJOs) in JSON.
+ */
+public final class POJOHelper {
+
+ private static final Logger LOG = LoggerFactory.getLogger(POJOHelper.class);
+
+ private static final ObjectMapper MAPPER;
+
+ static {
+ SimpleModule pojoModule = new SimpleModule("POJOModule", new Version(1, 0, 0, null, null, null));
+ pojoModule.addSerializer(GuardedString.class, new GuardedStringSerializer());
+ pojoModule.addSerializer(Attribute.class, new AttributeSerializer());
+ pojoModule.addSerializer(SyncToken.class, new SyncTokenSerializer());
+ pojoModule.addDeserializer(GuardedString.class, new GuardedStringDeserializer());
+ pojoModule.addDeserializer(Attribute.class, new AttributeDeserializer());
+ pojoModule.addDeserializer(SyncToken.class, new SyncTokenDeserializer());
+
+ MAPPER = new ObjectMapper();
+ MAPPER.registerModule(pojoModule);
+ MAPPER.registerModule(new AfterburnerModule());
+ }
+
+ public static String serialize(final Object object) {
+ String result = null;
+
+ try {
+ result = MAPPER.writeValueAsString(object);
+ } catch (Exception e) {
+ LOG.error("During serialization", e);
+ }
+
+ return result;
+ }
+
+ public static <T extends Object> T deserialize(final String serialized, final Class<T> reference) {
+ T result = null;
+
+ try {
+ result = MAPPER.readValue(serialized, reference);
+ } catch (Exception e) {
+ LOG.error("During deserialization", e);
+ }
+
+ return result;
+ }
+
+ private POJOHelper() {
+ }
+}
http://git-wip-us.apache.org/repos/asf/syncope/blob/556d5186/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/SyncTokenDeserializer.java
----------------------------------------------------------------------
diff --git a/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/SyncTokenDeserializer.java b/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/SyncTokenDeserializer.java
new file mode 100644
index 0000000..f0d19ec
--- /dev/null
+++ b/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/SyncTokenDeserializer.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.server.utils.serialization;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.io.IOException;
+import org.apache.commons.codec.binary.Base64;
+import org.identityconnectors.framework.common.objects.SyncToken;
+
+class SyncTokenDeserializer extends JsonDeserializer<SyncToken> {
+
+ @Override
+ public SyncToken deserialize(final JsonParser jp, final DeserializationContext ctx)
+ throws IOException, JsonProcessingException {
+
+ ObjectNode tree = jp.readValueAsTree();
+
+ Object value = null;
+ if (tree.has("value")) {
+ JsonNode node = tree.get("value");
+ value = node.isNull()
+ ? null
+ : node.isBoolean()
+ ? node.asBoolean()
+ : node.isDouble()
+ ? node.asDouble()
+ : node.isLong()
+ ? node.asLong()
+ : node.isInt()
+ ? node.asInt()
+ : node.asText();
+
+ if (value instanceof String) {
+ String base64 = (String) value;
+ if (Base64.isBase64(base64)) {
+ value = Base64.decodeBase64(base64);
+ }
+ }
+ }
+
+ return new SyncToken(value);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/syncope/blob/556d5186/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/SyncTokenSerializer.java
----------------------------------------------------------------------
diff --git a/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/SyncTokenSerializer.java b/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/SyncTokenSerializer.java
new file mode 100644
index 0000000..a9e1206
--- /dev/null
+++ b/syncope620/server/utils/src/main/java/org/apache/syncope/server/utils/serialization/SyncTokenSerializer.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.syncope.server.utils.serialization;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import java.io.IOException;
+import org.apache.commons.codec.binary.Base64;
+import org.identityconnectors.framework.common.objects.SyncToken;
+
+class SyncTokenSerializer extends JsonSerializer<SyncToken> {
+
+ @Override
+ public void serialize(final SyncToken source, final JsonGenerator jgen, final SerializerProvider sp)
+ throws IOException, JsonProcessingException {
+
+ jgen.writeStartObject();
+
+ jgen.writeFieldName("value");
+
+ if (source.getValue() == null) {
+ jgen.writeNull();
+ } else if (source.getValue() instanceof Boolean) {
+ jgen.writeBoolean((Boolean) source.getValue());
+ } else if (source.getValue() instanceof Double) {
+ jgen.writeNumber((Double) source.getValue());
+ } else if (source.getValue() instanceof Long) {
+ jgen.writeNumber((Long) source.getValue());
+ } else if (source.getValue() instanceof Integer) {
+ jgen.writeNumber((Integer) source.getValue());
+ } else if (source.getValue() instanceof byte[]) {
+ jgen.writeString(Base64.encodeBase64String((byte[]) source.getValue()));
+ } else {
+ jgen.writeString(source.getValue().toString());
+ }
+
+ jgen.writeEndObject();
+ }
+
+}