Search in sources :

Example 86 with HttpEntity

use of org.springframework.http.HttpEntity in project spring-data-document-examples by spring-projects.

the class FormHttpMessageConverter method writeParts.

private void writeParts(OutputStream os, MultiValueMap<String, Object> parts, byte[] boundary) throws IOException {
    for (Map.Entry<String, List<Object>> entry : parts.entrySet()) {
        String name = entry.getKey();
        for (Object part : entry.getValue()) {
            writeBoundary(boundary, os);
            HttpEntity entity = getEntity(part);
            writePart(name, entity, os);
            writeNewLine(os);
        }
    }
}
Also used : HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 87 with HttpEntity

use of org.springframework.http.HttpEntity in project androidannotations by androidannotations.

the class MyServiceTest method cookieInUrl.

@Test
public void cookieInUrl() {
    final String xtValue = "1234";
    final String sjsaidValue = "7890";
    final String locationValue = "somePlace";
    final int yearValue = 2013;
    MyService_ myService = new MyService_(null);
    RestTemplate restTemplate = mock(RestTemplate.class);
    myService.setRestTemplate(restTemplate);
    addPendingResponse("{'id':1,'name':'event1'}");
    // normally this is set by a call like authenticate()
    // which is annotated with @SetsCookie
    myService.setCookie("xt", xtValue);
    myService.setCookie("sjsaid", sjsaidValue);
    myService.setHttpBasicAuth("fancyUser", "fancierPassword");
    myService.getEventsVoid(locationValue, yearValue);
    ArgumentMatcher<HttpEntity<Void>> matcher = new ArgumentMatcher<HttpEntity<Void>>() {

        @Override
        public boolean matches(HttpEntity<Void> argument) {
            final String expected = "sjsaid=" + sjsaidValue + ";";
            return expected.equals(argument.getHeaders().get("Cookie").get(0));
        }
    };
    Map<String, Object> urlVariables = new HashMap<String, Object>();
    urlVariables.put("location", locationValue);
    urlVariables.put("year", yearValue);
    urlVariables.put("xt", xtValue);
    verify(restTemplate).exchange(Matchers.anyString(), Matchers.<HttpMethod>any(), argThat(matcher), Matchers.<Class<Object>>any(), eq(urlVariables));
}
Also used : HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) ArgumentMatcher(org.mockito.ArgumentMatcher) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Example 88 with HttpEntity

use of org.springframework.http.HttpEntity in project c4sg-services by Code4SocialGood.

the class UserController method retrieveProjectImage.

@CrossOrigin
@RequestMapping(value = "/{id}/resume", method = RequestMethod.GET)
@ApiOperation(value = "Retrieves user resume")
@ResponseBody
public HttpEntity<byte[]> retrieveProjectImage(@ApiParam(value = "User id to get resume for", required = true) @PathVariable("id") int id) {
    File resume = new File(userService.getResumeUploadPath(id));
    try {
        FileInputStream fileInputStreamReader = new FileInputStream(resume);
        byte[] bytes = new byte[(int) resume.length()];
        fileInputStreamReader.read(bytes);
        fileInputStreamReader.close();
        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_PDF);
        header.setContentLength(bytes.length);
        return new HttpEntity<byte[]>(bytes, header);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream)

Example 89 with HttpEntity

use of org.springframework.http.HttpEntity in project cloudstack by apache.

the class Force10BaremetalSwitchBackend method prepareVlan.

@Override
public void prepareVlan(BaremetalVlanStruct struct) {
    String link = buildLink(struct.getSwitchIp(), String.format("/api/running/ftos/interface/vlan/%s", struct.getVlan()));
    HttpHeaders headers = createBasicAuthenticationHeader(struct);
    HttpEntity<String> request = new HttpEntity<>(headers);
    ResponseEntity rsp = rest.exchange(link, HttpMethod.GET, request, String.class);
    logger.debug(String.format("http get: %s", link));
    if (rsp.getStatusCode() == HttpStatus.NOT_FOUND) {
        PortInfo port = new PortInfo(struct);
        XmlObject xml = new XmlObject("vlan").putElement("vlan-id", new XmlObject("vlan-id").setText(String.valueOf(struct.getVlan()))).putElement("untagged", new XmlObject("untagged").putElement(port.interfaceType, new XmlObject(port.interfaceType).putElement("name", new XmlObject("name").setText(port.port)))).putElement("shutdown", new XmlObject("shutdown").setText("false"));
        request = new HttpEntity<>(xml.dump(), headers);
        link = buildLink(struct.getSwitchIp(), String.format("/api/running/ftos/interface/"));
        logger.debug(String.format("http get: %s, body: %s", link, request));
        rsp = rest.exchange(link, HttpMethod.POST, request, String.class);
        if (!successHttpStatusCode.contains(rsp.getStatusCode())) {
            throw new CloudRuntimeException(String.format("unable to create vlan[%s] on force10 switch[ip:%s]. HTTP status code:%s, body dump:%s", struct.getVlan(), struct.getSwitchIp(), rsp.getStatusCode(), rsp.getBody()));
        } else {
            logger.debug(String.format("successfully programmed vlan[%s] on Force10[ip:%s, port:%s]. http response[status code:%s, body:%s]", struct.getVlan(), struct.getSwitchIp(), struct.getPort(), rsp.getStatusCode(), rsp.getBody()));
        }
    } else if (successHttpStatusCode.contains(rsp.getStatusCode())) {
        PortInfo port = new PortInfo(struct);
        XmlObject xml = XmlObjectParser.parseFromString((String) rsp.getBody());
        List<XmlObject> ports = xml.getAsList("untagged.tengigabitethernet");
        ports.addAll(xml.<XmlObject>getAsList("untagged.gigabitethernet"));
        ports.addAll(xml.<XmlObject>getAsList("untagged.fortyGigE"));
        for (XmlObject pxml : ports) {
            XmlObject name = pxml.get("name");
            if (port.port.equals(name.getText())) {
                logger.debug(String.format("port[%s] has joined in vlan[%s], no need to program again", struct.getPort(), struct.getVlan()));
                return;
            }
        }
        xml.removeElement("mtu");
        xml.setText(null);
        XmlObject tag = xml.get("untagged");
        if (tag == null) {
            tag = new XmlObject("untagged");
            xml.putElement("untagged", tag);
        }
        tag.putElement(port.interfaceType, new XmlObject(port.interfaceType).putElement("name", new XmlObject("name").setText(port.port)));
        request = new HttpEntity<>(xml.dump(), headers);
        link = buildLink(struct.getSwitchIp(), String.format("/api/running/ftos/interface/vlan/%s", struct.getVlan()));
        logger.debug(String.format("http get: %s, body: %s", link, request));
        rsp = rest.exchange(link, HttpMethod.PUT, request, String.class);
        if (!successHttpStatusCode.contains(rsp.getStatusCode())) {
            throw new CloudRuntimeException(String.format("failed to program vlan[%s] for port[%s] on force10[ip:%s]. http status:%s, body dump:%s", struct.getVlan(), struct.getPort(), struct.getSwitchIp(), rsp.getStatusCode(), rsp.getBody()));
        } else {
            logger.debug(String.format("successfully join port[%s] into vlan[%s] on Force10[ip:%s]. http response[status code:%s, body:%s]", struct.getPort(), struct.getVlan(), struct.getSwitchIp(), rsp.getStatusCode(), rsp.getBody()));
        }
    } else {
        throw new CloudRuntimeException(String.format("force10[ip:%s] returns unexpected error[%s] when http getting %s, body dump:%s", struct.getSwitchIp(), rsp.getStatusCode(), link, rsp.getBody()));
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XmlObject(com.cloud.utils.xmlobject.XmlObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 90 with HttpEntity

use of org.springframework.http.HttpEntity in project geode by apache.

the class ClientHttpRequestJUnitTest method testCreateRequestEntityForPost.

@Test
@SuppressWarnings("unchecked")
public void testCreateRequestEntityForPost() throws Exception {
    final Link expectedLink = new Link("post", toUri("http://host.domain.com:8080/app/libraries/{name}/books"), HttpMethod.POST);
    final ClientHttpRequest request = new ClientHttpRequest(expectedLink);
    assertEquals(expectedLink, request.getLink());
    final MultiValueMap<String, Object> expectedRequestParameters = new LinkedMultiValueMap<String, Object>(4);
    expectedRequestParameters.add("author", "Douglas Adams");
    expectedRequestParameters.add("title", "The Hitchhiker's Guide to the Galaxy");
    expectedRequestParameters.add("year", "1979");
    expectedRequestParameters.add("isbn", "0345453743");
    request.addHeaderValues(HttpHeader.CONTENT_TYPE.getName(), MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    request.addParameterValues("author", expectedRequestParameters.getFirst("author"));
    request.addParameterValues("title", expectedRequestParameters.getFirst("title"));
    request.addParameterValues("year", expectedRequestParameters.getFirst("year"));
    request.addParameterValues("isbn", expectedRequestParameters.getFirst("isbn"));
    final HttpEntity<MultiValueMap<String, Object>> requestEntity = (HttpEntity<MultiValueMap<String, Object>>) request.createRequestEntity();
    assertNotNull(requestEntity);
    assertNotNull(requestEntity.getHeaders());
    assertEquals(MediaType.APPLICATION_FORM_URLENCODED, requestEntity.getHeaders().getContentType());
    assertEquals(expectedRequestParameters, requestEntity.getBody());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Link(org.apache.geode.management.internal.web.domain.Link) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)104 HttpHeaders (org.springframework.http.HttpHeaders)81 Test (org.junit.Test)46 RestTemplate (org.springframework.web.client.RestTemplate)17 URI (java.net.URI)15 ArrayList (java.util.ArrayList)13 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 Map (java.util.Map)11 MediaType (org.springframework.http.MediaType)11 HashMap (java.util.HashMap)10 List (java.util.List)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)7 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)7 HttpStatus (org.springframework.http.HttpStatus)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6