Search in sources :

Example 16 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project ma-modules-public by infiniteautomation.

the class MangoV2WebSocketHandler method sendMessageUsingView.

protected void sendMessageUsingView(WebSocketSession session, WebSocketMessage message, Class<?> view) throws JsonProcessingException {
    ObjectWriter objectWriter = this.jacksonMapper.writerWithView(view);
    this.sendStringMessage(session, objectWriter.writeValueAsString(message));
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 17 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project ma-modules-public by infiniteautomation.

the class UserFunctionalTests method testAdminCreateUser.

/**
 * Test Creating a User
 * TODO This test fails!!!! Because we don't render the password in the JSON property yet. :(
 */
public void testAdminCreateUser() {
    User standardUser = UserTestData.standardUser();
    User adminUser = UserTestData.adminUser();
    List<User> users = new ArrayList<>();
    users.add(standardUser);
    // This will ensure that the getUsers() method returns
    // the mock list of users
    when(userDao.getUser(standardUser.getUsername())).thenReturn(null);
    ObjectWriter writer = this.objectMapper.writerWithView(JsonViews.Test.class);
    try {
        String userJson = writer.writeValueAsString(new UserModel(standardUser));
        this.mockMvc.perform(post("/v1/users/").content(userJson).contentType(MediaType.APPLICATION_JSON).sessionAttr("sessionUser", adminUser).accept(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isCreated());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) User(com.serotonin.m2m2.vo.User) JsonViews(com.serotonin.m2m2.web.mvc.rest.v1.mapping.JsonViews) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 18 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project ma-modules-public by infiniteautomation.

the class DataSourceFunctionalTests method testAdminUpdate.

/**
 * Test udpating a mock data source
 */
public void testAdminUpdate() {
    DataSourceVO ds = DataSourceTestData.mockDataSource();
    when(dataSourceDao.getByXid(ds.getXid())).thenReturn(ds);
    User adminUser = UserTestData.adminUser();
    ObjectWriter writer = this.objectMapper.writerWithView(JsonViews.Test.class);
    try {
        String userJson = writer.writeValueAsString(new MockDataSourceModel((MockDataSourceVO) ds));
        this.mockMvc.perform(put("/v1/dataSources/" + ds.getXid()).content(userJson).contentType(MediaType.APPLICATION_JSON).sessionAttr("sessionUser", adminUser).accept(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isCreated());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) User(com.serotonin.m2m2.vo.User) JsonViews(com.serotonin.m2m2.web.mvc.rest.v1.mapping.JsonViews) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) MockDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.MockDataSourceModel)

Example 19 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project commons by craftercms.

the class CrafterJackson2MessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.getObjectMapper().getFactory().createGenerator(outputMessage.getBody(), encoding);
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.getObjectMapper().isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }
    try {
        if (this.jsonPrefix != null) {
            jsonGenerator.writeRaw(this.jsonPrefix);
        }
        runAnnotations(object);
        ObjectWriter writer = this.getObjectMapper().writer(filter);
        writer.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 20 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project kylo by Teradata.

the class JwtRememberMeServices method generatePrincipalsToken.

/**
 * Serializes principal information derived from the supplied authorities as a JSON string
 * that can be deserialized back into a set of authorities containing those Principal objects.
 * @param collection the authorities produced after login
 * @return a JSON string describing the principals derived from the authorities
 */
protected String generatePrincipalsToken(Collection<? extends GrantedAuthority> authorities) {
    PrincipalsToken token = new PrincipalsToken();
    ObjectWriter writer = mapper.writer().forType(PrincipalsToken.class);
    for (GrantedAuthority authority : authorities) {
        token.add(authority);
    }
    try {
        return writer.writeValueAsString(token);
    } catch (JsonProcessingException e) {
        // Shouldn't really happen
        throw new IllegalStateException("Unable to serialize principals for JWT token", e);
    }
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) JaasGrantedAuthority(org.springframework.security.authentication.jaas.JaasGrantedAuthority) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)140 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)61 IOException (java.io.IOException)31 Test (org.junit.Test)30 File (java.io.File)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)15 ArrayList (java.util.ArrayList)12 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)11 JavaType (com.fasterxml.jackson.databind.JavaType)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 StringWriter (java.io.StringWriter)7 Map (java.util.Map)7 JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)6 FileInputStream (java.io.FileInputStream)6