Skip to content

Commit

Permalink
Merge pull request apereo#741 from Unicon/json-serialization-ldapmapper
Browse files Browse the repository at this point in the history
JSON serialization of RegisteredService into LDAP
  • Loading branch information
SavvasMisaghMoayyed committed Dec 1, 2014
2 parents 7c26449 + 38525b1 commit cf6b525
Show file tree
Hide file tree
Showing 16 changed files with 445 additions and 546 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void setSalt(final String salt) {
LOGGER.warn("setSalt() is deprecated and will be removed. Use the constructor instead.");
}

public byte[] getSalt() {
return salt;
}

@Override
public String generate(final Principal principal, final Service service) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import javax.persistence.Inheritance;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -105,7 +104,7 @@ public abstract class AbstractRegisteredService implements RegisteredService, Co
* The logout type of the service.
* The default logout type is the back channel one.
*/
@Transient
@Column(name = "logout_type", nullable = true)
private LogoutType logoutType = LogoutType.BACK_CHANNEL;

@Lob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public AnonymousRegisteredServiceUsernameAttributeProvider() {
public AnonymousRegisteredServiceUsernameAttributeProvider(@NotNull final PersistentIdGenerator persistentIdGenerator) {
this.persistentIdGenerator = persistentIdGenerator;
}


public PersistentIdGenerator getPersistentIdGenerator() {
return this.persistentIdGenerator;
}

@Override
public String resolveUsername(final Principal principal, final Service service) {
final String id = this.persistentIdGenerator.generate(principal, service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jasig.cas.util.LockedOutputStream;
import org.jasig.cas.util.services.JsonSerializer;
import org.jasig.cas.util.JsonSerializer;
import org.jasig.cas.util.services.RegisteredServiceJsonSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String resolveUsername(final Principal principal, final Service service)
}

logger.debug("Principal id to return is [{}]. The default principal id is [{}].",
principal.getId(), principalId);
principalId, principal.getId());
return principalId;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Licensed to Apereo under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Apereo 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 the following location:
*
* 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.jasig.cas.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

/**
* Generic class to serialize objects to/from JSON based on jackson.
* @author Misagh Moayyed
* @since 4.1
*/
public abstract class AbstractJacksonBackedJsonSerializer<T> implements JsonSerializer<T> {
private static final long serialVersionUID = -8415599777321259365L;

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractJacksonBackedJsonSerializer.class);

/**
* The Pretty printer.
*/
private final PrettyPrinter prettyPrinter;

/**
* The Object mapper.
*/
private final ObjectMapper objectMapper;

/**
* Instantiates a new Registered service json serializer.
* Uses the {@link com.fasterxml.jackson.core.util.DefaultPrettyPrinter} for formatting.
*/
public AbstractJacksonBackedJsonSerializer() {
this(new DefaultPrettyPrinter());
}

/**
* Instantiates a new Registered service json serializer.
*
* @param prettyPrinter the pretty printer
*/
public AbstractJacksonBackedJsonSerializer(final PrettyPrinter prettyPrinter) {
this.objectMapper = initializeObjectMapper();
this.prettyPrinter = prettyPrinter;
}

/**
* Instantiates a new Registered service json serializer.
*
* @param objectMapper the object mapper
* @param prettyPrinter the pretty printer
*/
public AbstractJacksonBackedJsonSerializer(final ObjectMapper objectMapper, final PrettyPrinter prettyPrinter) {
this.objectMapper = objectMapper;
this.prettyPrinter = prettyPrinter;
}

@Override
public T fromJson(final String json) {
try {
return this.objectMapper.readValue(json, getTypeToSerialize());
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

@Override
public T fromJson(final File json) {
try {
return this.objectMapper.readValue(json, getTypeToSerialize());
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

@Override
public T fromJson(final Reader json) {
try {
return this.objectMapper.readValue(json, getTypeToSerialize());
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

@Override
public T fromJson(final InputStream json) {
try {

return this.objectMapper.readValue(json, getTypeToSerialize());
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void toJson(final OutputStream out, final T object) {
try {
this.objectMapper.writer(this.prettyPrinter).writeValue(out, object);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void toJson(final Writer out, final T object) {
try {
this.objectMapper.writer(this.prettyPrinter).writeValue(out, object);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void toJson(final File out, final T object) {
try {
this.objectMapper.writer(this.prettyPrinter).writeValue(out, object);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}

/**
* Initialize object mapper.
*
* @return the object mapper
*/
protected ObjectMapper initializeObjectMapper() {
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
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;
}

/**
* Gets type to serialize.
*
* @return the type to serialize
*/
protected abstract Class<T> getTypeToSerialize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.cas.util.services;
package org.jasig.cas.util;

import java.io.File;
import java.io.InputStream;
Expand All @@ -28,33 +28,34 @@
/**
* Interface to define operations needed to map objects from/to JSON clobs.
* @author Misagh Moayyed
* @param <T> the type parameter
* @since 4.1.0
*/
public interface JsonSerializer<T> extends Serializable {
/**
* Create the object type from the given JSON string.
* @param json the jsob string
* @param json the json string
* @return the object instance constructed from JSON
*/
T fromJson(String json);

/**
* Create the object type from the given JSON reader.
* @param json the jsob string
* @param json the json string
* @return the object instance constructed from JSON
*/
T fromJson(Reader json);

/**
* Create the object type from the given JSON stream.
* @param json the jsob string
* @param json the json string
* @return the object instance constructed from JSON
*/
T fromJson(InputStream json);

/**
* Create the object type from the given JSON file.
* @param json the jsob string
* @param json the json string
* @return the object instance constructed from JSON
*/
T fromJson(File json);
Expand Down
Loading

0 comments on commit cf6b525

Please sign in to comment.