Search in sources :

Example 6 with PropertyAccessor

use of org.springframework.beans.PropertyAccessor in project dkpro-lab by dkpro.

the class DefaultLifeCycleManager method configure.

@Override
public void configure(TaskContext aParentContext, Task aTask, Map<String, Object> aConfiguration) {
    PropertyAccessor paBean = PropertyAccessorFactory.forBeanPropertyAccess(aTask);
    PropertyAccessor paDirect = PropertyAccessorFactory.forDirectFieldAccess(aTask);
    for (Entry<String, Object> property : aConfiguration.entrySet()) {
        String key = property.getKey();
        Object value = property.getValue();
        // a non-default name and might apply.
        for (String prop : ParameterUtil.findBeanPropertiesWithName(aTask, key)) {
            // Try setter - there may be extra logic in the setter
            if (paBean.isWritableProperty(prop)) {
                paBean.setPropertyValue(prop, value);
            } else // Otherwise try direct access
            if (paDirect.isWritableProperty(prop)) {
                paDirect.setPropertyValue(prop, value);
            }
        }
        // Try setter - there may be extra logic in the setter
        if (paBean.isWritableProperty(key)) {
            paBean.setPropertyValue(key, value);
        } else // Otherwise try direct access
        if (paDirect.isWritableProperty(key)) {
            paDirect.setPropertyValue(key, value);
        }
    }
    if (aTask instanceof ConfigurationAware) {
        ((ConfigurationAware) aTask).setConfiguration(aConfiguration);
    }
    if (aParentContext != null) {
        aParentContext.message("Injected parameters into [" + aTask.getType() + "]");
    }
}
Also used : PropertyAccessor(org.springframework.beans.PropertyAccessor) ConfigurationAware(org.dkpro.lab.task.ConfigurationAware)

Example 7 with PropertyAccessor

use of org.springframework.beans.PropertyAccessor in project spring-integration by spring-projects.

the class HttpProxyScenarioTests method testHttpProxyScenario.

@Test
public void testHttpProxyScenario() throws Exception {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.RFC_1123_DATE_TIME;
    ZoneId GMT = ZoneId.of("GMT");
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MILLISECOND, 0);
    final long ifModifiedSince = c.getTimeInMillis();
    Instant instant = Instant.ofEpochMilli(ifModifiedSince);
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, GMT);
    String ifModifiedSinceValue = dateTimeFormatter.format(zonedDateTime);
    c.add(Calendar.DATE, -1);
    long ifUnmodifiedSince = c.getTimeInMillis();
    instant = Instant.ofEpochMilli(ifUnmodifiedSince);
    zonedDateTime = ZonedDateTime.ofInstant(instant, GMT);
    final String ifUnmodifiedSinceValue = dateTimeFormatter.format(zonedDateTime);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
    request.setQueryString("foo=bar&FOO=BAR");
    request.addHeader("If-Modified-Since", ifModifiedSinceValue);
    request.addHeader("If-Unmodified-Since", ifUnmodifiedSinceValue);
    request.addHeader("Connection", "Keep-Alive");
    request.setContentType("text/plain");
    Object handler = this.handlerMapping.getHandler(request).getHandler();
    assertNotNull(handler);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RestTemplate template = Mockito.spy(new RestTemplate());
    final String contentDispositionValue = "attachment; filename=\"test.txt\"";
    Mockito.doAnswer(invocation -> {
        URI uri = invocation.getArgument(0);
        assertEquals(new URI("http://testServer/test?foo=bar&FOO=BAR"), uri);
        HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
        HttpHeaders httpHeaders = httpEntity.getHeaders();
        assertEquals(ifModifiedSince, httpHeaders.getIfModifiedSince());
        assertEquals(ifUnmodifiedSinceValue, httpHeaders.getFirst("If-Unmodified-Since"));
        assertEquals("Keep-Alive", httpHeaders.getFirst("Connection"));
        MultiValueMap<String, String> responseHeaders = new LinkedMultiValueMap<String, String>(httpHeaders);
        responseHeaders.set("Connection", "close");
        responseHeaders.set("Content-Disposition", contentDispositionValue);
        return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
    }).when(template).exchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), (Class<?>) isNull());
    PropertyAccessor dfa = new DirectFieldAccessor(this.handler);
    dfa.setPropertyValue("restTemplate", template);
    RequestAttributes attributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(attributes);
    this.handlerAdapter.handle(request, response, handler);
    assertEquals(ifModifiedSinceValue, response.getHeaderValue("If-Modified-Since"));
    assertEquals(ifUnmodifiedSinceValue, response.getHeaderValue("If-Unmodified-Since"));
    assertEquals("close", response.getHeaderValue("Connection"));
    assertEquals(contentDispositionValue, response.getHeader("Content-Disposition"));
    assertEquals("text/plain", response.getContentType());
    Message<?> message = this.checkHeadersChannel.receive(2000);
    MessageHeaders headers = message.getHeaders();
    assertEquals(ifModifiedSince, headers.get("If-Modified-Since"));
    assertEquals(ifUnmodifiedSince, headers.get("If-Unmodified-Since"));
    RequestContextHolder.resetRequestAttributes();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) URI(java.net.URI) ZonedDateTime(java.time.ZonedDateTime) MessageHeaders(org.springframework.messaging.MessageHeaders) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) PropertyAccessor(org.springframework.beans.PropertyAccessor) ZoneId(java.time.ZoneId) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Calendar(java.util.Calendar) Instant(java.time.Instant) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ResponseEntity(org.springframework.http.ResponseEntity) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RestTemplate(org.springframework.web.client.RestTemplate) DateTimeFormatter(java.time.format.DateTimeFormatter) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 8 with PropertyAccessor

use of org.springframework.beans.PropertyAccessor in project spring-integration by spring-projects.

the class HttpProxyScenarioTests method testHttpMultipartProxyScenario.

@Test
public void testHttpMultipartProxyScenario() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/testmp");
    request.addHeader("Connection", "Keep-Alive");
    request.setContentType("multipart/form-data;boundary=----WebKitFormBoundarywABD2xqC1FLBijlQ");
    request.setContent("foo".getBytes());
    Object handler = this.handlerMapping.getHandler(request).getHandler();
    assertNotNull(handler);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RestTemplate template = Mockito.spy(new RestTemplate());
    Mockito.doAnswer(invocation -> {
        URI uri = invocation.getArgument(0);
        assertEquals(new URI("http://testServer/testmp"), uri);
        HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
        HttpHeaders httpHeaders = httpEntity.getHeaders();
        assertEquals("Keep-Alive", httpHeaders.getFirst("Connection"));
        assertEquals("multipart/form-data;boundary=----WebKitFormBoundarywABD2xqC1FLBijlQ", httpHeaders.getContentType().toString());
        HttpEntity<?> entity = (HttpEntity<?>) invocation.getArguments()[2];
        assertThat(entity.getBody(), instanceOf(byte[].class));
        assertEquals("foo", new String((byte[]) entity.getBody()));
        MultiValueMap<String, String> responseHeaders = new LinkedMultiValueMap<String, String>(httpHeaders);
        responseHeaders.set("Connection", "close");
        responseHeaders.set("Content-Type", "text/plain");
        return new ResponseEntity<Object>(responseHeaders, HttpStatus.OK);
    }).when(template).exchange(Mockito.any(URI.class), Mockito.any(HttpMethod.class), Mockito.any(HttpEntity.class), (Class<?>) isNull());
    PropertyAccessor dfa = new DirectFieldAccessor(this.handlermp);
    dfa.setPropertyValue("restTemplate", template);
    RequestAttributes attributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(attributes);
    this.handlerAdapter.handle(request, response, handler);
    assertEquals("close", response.getHeaderValue("Connection"));
    assertEquals("text/plain", response.getContentType());
    RequestContextHolder.resetRequestAttributes();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) PropertyAccessor(org.springframework.beans.PropertyAccessor) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) URI(java.net.URI) ResponseEntity(org.springframework.http.ResponseEntity) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RestTemplate(org.springframework.web.client.RestTemplate) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 9 with PropertyAccessor

use of org.springframework.beans.PropertyAccessor in project spring-boot by spring-projects.

the class DeviceDelegatingViewResolverAutoConfigurationTests method overrideNormalPrefix.

@Test
public void overrideNormalPrefix() throws Exception {
    PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor("spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.normalPrefix:normal/");
    assertThat(accessor.getPropertyValue("normalPrefix")).isEqualTo("normal/");
}
Also used : PropertyAccessor(org.springframework.beans.PropertyAccessor) Test(org.junit.Test)

Example 10 with PropertyAccessor

use of org.springframework.beans.PropertyAccessor in project spring-boot by spring-projects.

the class DeviceDelegatingViewResolverAutoConfigurationTests method overrideEnableFallback.

@Test
public void overrideEnableFallback() throws Exception {
    PropertyAccessor accessor = getLiteDeviceDelegatingViewResolverAccessor("spring.mobile.devicedelegatingviewresolver.enabled:true", "spring.mobile.devicedelegatingviewresolver.enableFallback:true");
    assertThat(accessor.getPropertyValue("enableFallback")).isEqualTo(Boolean.TRUE);
}
Also used : PropertyAccessor(org.springframework.beans.PropertyAccessor) Test(org.junit.Test)

Aggregations

PropertyAccessor (org.springframework.beans.PropertyAccessor)13 Test (org.junit.Test)9 URI (java.net.URI)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMethod (org.springframework.http.HttpMethod)2 ResponseEntity (org.springframework.http.ResponseEntity)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 RestTemplate (org.springframework.web.client.RestTemplate)2 RequestAttributes (org.springframework.web.context.request.RequestAttributes)2 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)2 HasId (org.summerb.approaches.jdbccrud.api.dto.HasId)2 Ref (org.summerb.approaches.jdbccrud.api.dto.relations.Ref)2 WithId (io.syndesis.common.model.WithId)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1 ZonedDateTime (java.time.ZonedDateTime)1