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);
}
}
}
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));
}
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;
}
}
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()));
}
}
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());
}
Aggregations