Example usage for org.apache.commons.lang SerializationUtils serialize

List of usage examples for org.apache.commons.lang SerializationUtils serialize

Introduction

In this page you can find the example usage for org.apache.commons.lang SerializationUtils serialize.

Prototype

public static byte[] serialize(Serializable obj) 

Source Link

Document

Serializes an Object to a byte array for storage/serialization.

Usage

From source file:nl.surfnet.coin.api.oauth.OpenConextOauth1TokenServices.java

@Override
protected void storeToken(String value, OAuthProviderTokenImpl token) {
    Assert.notNull(token, "Token cannot be null");
    Assert.notNull(value, "token value cannot be null");
    Authentication userAuthentication = token.getUserAuthentication();
    String userId = null;/*w  w w  .java2 s . co m*/
    if (token.isAccessToken()) {
        String consumerKey = token.getConsumerKey();
        /*
         * get the client detail from Janus as we are unable to store them
         * somewhere along the 'road' and we cache this call anyway
         */
        ConsumerDetails consumerDetails = consumerDetailsService.loadConsumerByConsumerKey(consumerKey);
        if (consumerDetails instanceof OpenConextConsumerDetails) {
            OpenConextConsumerDetails extendedBaseConsumerDetails = (OpenConextConsumerDetails) consumerDetails;
            if (userAuthentication instanceof PreAuthenticatedAuthenticationToken) {
                PreAuthenticatedAuthenticationToken pre = (PreAuthenticatedAuthenticationToken) userAuthentication;
                Object principal = pre.getPrincipal();
                if (principal instanceof ClientMetaDataUser) {
                    ((ClientMetaDataUser) principal)
                            .setClientMetaData(extendedBaseConsumerDetails.getClientMetaData());
                    userId = ((ClientMetaDataUser) principal).getUsername();
                } else if (principal instanceof SAMLAuthenticationToken) {
                    ((SAMLAuthenticationToken) principal)
                            .setClientMetaData(extendedBaseConsumerDetails.getClientMetaData());
                    userId = ((SAMLAuthenticationToken) principal).getName();
                } else {
                    throw new RuntimeException(
                            "The principal on the PreAuthenticatedAuthenticationToken is of the type '"
                                    + (principal != null ? principal.getClass() : "null")
                                    + "'. Required is a (sub)class of ClientMetaDataUser or a (sub)class of SAMLAuthenticationToken");
                }
            } else if (userAuthentication instanceof SAMLAuthenticationToken) {
                SAMLAuthenticationToken samlToken = (SAMLAuthenticationToken) userAuthentication;
                samlToken.setClientMetaData(extendedBaseConsumerDetails.getClientMetaData());
                userId = samlToken.getName();
            } else {
                throw new RuntimeException("The userAuthentication is of the type '"
                        + (userAuthentication != null ? userAuthentication.getClass() : "null")
                        + "'. Required is a (sub)class of PreAuthenticatedAuthenticationToken or SAMLAuthenticationToken");
            }
        } else {
            throw new RuntimeException("The consumerDetails is of the type '"
                    + (consumerDetails != null ? consumerDetails.getClass() : "null")
                    + "'. Required is a (sub)class of ExtendedBaseConsumerDetails");
        }
    }
    jdbcTemplate.update(deleteTokenSql, value);
    jdbcTemplate.update(insertTokenSql, value, token.getCallbackUrl(), token.getVerifier(), token.getSecret(),
            token.getConsumerKey(), userId, token.isAccessToken(), token.getTimestamp(),
            SerializationUtils.serialize(userAuthentication));
}

From source file:nl.surfnet.coin.api.service.JanusClientDetailsServiceTest.java

@Test
public void serializeMetaData() {
    super.setResponseResource(new ClassPathResource("janus/janus-response-metadata.json"));
    EntityMetadata metaData = restClient.getMetadataByEntityId("http://dummy-entity-id");
    byte[] serialize = SerializationUtils.serialize(metaData);

}

From source file:nl.surfnet.coin.teams.service.impl.GroupProviderServiceSQLImplTest.java

/**
 * The {@link GroupProvider} are placed in the session and therefore need to be {@link Serializable}
 *//*from w  w  w  .ja v  a  2s . com*/
@Test
public void serializeGroupProvider() {
    List<GroupProvider> all = groupProviderServiceSQL.getAllGroupProviders();
    byte[] serialize = SerializationUtils.serialize((Serializable) all);

}

From source file:org.apache.camel.component.amqp.AMQPRouteTest.java

@Test
public void testJmsRouteWithByteArrayMessage() throws Exception {
    PurchaseOrder aPO = new PurchaseOrder("Beer", 10);
    byte[] expectedBody = SerializationUtils.serialize(aPO);

    resultEndpoint.expectedBodiesReceived(expectedBody);
    resultEndpoint.message(0).header("cheese").isEqualTo(123);

    sendExchange(expectedBody);//  w  ww.j  a  v a 2  s.  c o m

    resultEndpoint.assertIsSatisfied();
}

From source file:org.apache.camel.component.kafka.KafkaComponentUtil.java

/**
 * Utility method to serialize the whole exchange.
 *
 * @param exchange Exchange//from  w ww . j  a  v a  2s.  c om
 * @return  byte array
 */
public static byte[] serializeExchange(final Exchange exchange) {

    return SerializationUtils.serialize(DefaultExchangeHolder.marshal(exchange));
}

From source file:org.apache.camel.component.kafka.KafkaComponentUtil.java

/**
 * Utility method to serialize the whole exchange.
 *
 * @param exchange//from  w  ww .j a v  a  2s . co m
 * @return
 */
public static byte[] serializeBody(final Exchange exchange) {

    return SerializationUtils.serialize(exchange.getIn().getBody(byte[].class));
}

From source file:org.apache.crunch.impl.mem.collect.MemCollection.java

private <S, T> DoFn<S, T> verifySerializable(String name, DoFn<S, T> doFn) {
    try {/*from   w  ww. ja  v  a2  s  . c  o  m*/
        return (DoFn<S, T>) deserialize(SerializationUtils.serialize(doFn));
    } catch (SerializationException e) {
        throw new IllegalStateException(
                doFn.getClass().getSimpleName() + " named '" + name + "' cannot be serialized", e);
    }
}

From source file:org.apache.directory.server.ldap.handlers.extended.StoredProcedureExtendedOperationHandler.java

public void handleExtendedOperation(LdapSession session, StoredProcedureRequest req) throws Exception {
    String procedure = req.getProcedureSpecification();
    Entry spUnit = manager.findStoredProcUnit(session.getCoreSession(), procedure);
    StoredProcEngine engine = manager.getStoredProcEngineInstance(spUnit);

    List<Object> valueList = new ArrayList<>(req.size());

    for (int ii = 0; ii < req.size(); ii++) {
        byte[] serializedValue = (byte[]) req.getParameterValue(ii);
        Object value = SerializationUtils.deserialize(serializedValue);

        if (value.getClass().equals(LdapContextParameter.class)) {
            String paramCtx = ((LdapContextParameter) value).getValue();
            value = session.getCoreSession().lookup(new Dn(paramCtx));
        }//from www.j a  v a  2s . c  o m

        valueList.add(value);
    }

    Object[] values = valueList.toArray(EMPTY_CLASS_ARRAY);
    Object response = engine.invokeProcedure(session.getCoreSession(), procedure, values);
    byte[] serializedResponse = SerializationUtils.serialize((Serializable) response);
    StoredProcedureResponse resp = LdapApiServiceFactory.getSingleton()
            .newExtendedResponse(req.getRequestName(), req.getMessageId(), serializedResponse);
    session.getIoSession().write(resp);
}

From source file:org.apache.drill.exec.store.mongo.config.MongoPStore.java

private V value(Object obj) {
    try {/* www.ja  v a2 s.  c  o  m*/
        byte[] serialize = SerializationUtils.serialize((Serializable) obj);
        return config.getSerializer().deserialize(serialize);
    } catch (IOException e) {
        throw new DrillRuntimeException(e.getMessage(), e);
    }
}

From source file:org.apache.hama.ml.ann.NeuralNetwork.java

@Override
public void write(DataOutput output) throws IOException {
    // write model type
    WritableUtils.writeString(output, modelType);
    // write learning rate
    output.writeDouble(learningRate);/*ww w .j  a va 2  s . c  o m*/
    // write model path
    if (this.modelPath != null) {
        WritableUtils.writeString(output, modelPath);
    } else {
        WritableUtils.writeString(output, "null");
    }

    // serialize the class
    Class<? extends FeatureTransformer> featureTransformerCls = this.featureTransformer.getClass();
    byte[] featureTransformerBytes = SerializationUtils.serialize(featureTransformerCls);
    output.writeInt(featureTransformerBytes.length);
    output.write(featureTransformerBytes);
}