Spring Framework HttpHeaders - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Spring Framework HttpHeaders class.

This code example shows how to use the following methods:getContentLength, setContentLength
	} 
 
	@Override 
	protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers) throws IOException {
		byte[] bytes = this.bufferedOutput.toByteArray();
		if (headers.getContentLength() == -1) {
			headers.setContentLength(bytes.length);
		} 
		ListenableFuture<ClientHttpResponse> result = executeInternal(headers, bytes);
		this.bufferedOutput = null;
		return result;
	} 
 
	/** 
	 * Abstract template method that writes the given headers and content to the HTTP request. 
	 * @param headers the HTTP headers 
	 * @param bufferedOutput the body content 
	 * @return the response object for the executed request 
	 */ 
	protected abstract ListenableFuture<ClientHttpResponse> executeInternal(
7
CodeRank
This code example shows how to use the following methods:containsKey, get, getContentType
		mockRequest.setContentType("text/plain"); 
		mockRequest.setCharacterEncoding("UTF-8"); 
 
		HttpHeaders headers = request.getHeaders(); 
		assertNotNull("No HttpHeaders returned", headers); 
		assertTrue("Invalid headers returned", headers.containsKey(headerName)); 
		List<String> headerValues = headers.get(headerName); 
		assertEquals("Invalid header values returned", 2, headerValues.size()); 
		assertTrue("Invalid header values returned", headerValues.contains(headerValue1)); 
		assertTrue("Invalid header values returned", headerValues.contains(headerValue2)); 
		assertEquals("Invalid Content-Type", new MediaType("text", "plain", Charset.forName("UTF-8")), 
				headers.getContentType()); 
	} 
 
	@Test 
	public void getHeadersWithEmptyContentTypeAndEncoding() throws Exception { 
		String headerName = "MyHeader"; 
		String headerValue1 = "value1"; 
		String headerValue2 = "value2"; 
		mockRequest.addHeader(headerName, headerValue1); 
7
CodeRank
This code example shows how to use the following methods:getContentLength, setContentLength
	} 
 
	@Override 
	protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
		byte[] bytes = this.bufferedOutput.toByteArray();
		if (headers.getContentLength() == -1) {
			headers.setContentLength(bytes.length);
		} 
		ClientHttpResponse result = executeInternal(headers, bytes);
		this.bufferedOutput = null;
		return result;
	} 
 
	/** 
	 * Abstract template method that writes the given headers and content to the HTTP request. 
	 * @param headers the HTTP headers 
	 * @param bufferedOutput the body content 
	 * @return the response object for the executed request 
	 */ 
	protected abstract ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput)
7
CodeRank
This code example shows how to use the following methods:set
 
		HttpHeaders response = serverRunning.postForHeaders(
				"/sparklr2/login.do", form);
 
		cookie = response.getFirst("Set-Cookie");
		HttpHeaders headers = new HttpHeaders();
		headers.set("Cookie", cookie);
 
		serverRunning.getForString(location, headers);
		// Should be on user approval page now 
		form = new LinkedMultiValueMap<String, String>();
		form.add("user_oauth_approval", "true");
		form.add("scope.read", "true");
		response = serverRunning.postForHeaders("/sparklr2/oauth/authorize",
				form, headers);
 
		return response.getLocation().toString();
	} 
 
} 
7
CodeRank
This code example shows how to use the following methods:putAll, set
		builder.append(typeName+" ");
		builder.append("realm=\"" + realmName + "\"");
		if (existing!=null) {
			builder.append(", "+existing);
		} 
		HttpHeaders update = new HttpHeaders();
		update.putAll(response.getHeaders());
		update.set("WWW-Authenticate", builder.toString());
		return new ResponseEntity<OAuth2Exception>(response.getBody(), update, response.getStatusCode());
	} 
 
	private String extractTypePrefix(String header) {
		String existing = header;
		String[] tokens = existing.split(" +");
		if (tokens.length > 1 && !tokens[0].endsWith(",")) {
			existing = StringUtils.arrayToDelimitedString(tokens, " ").substring(existing.indexOf(" ") + 1);
		} 
		return existing;
	} 
 
7
CodeRank
This code example shows how to use the following methods:add, setContentType
	@Test 
	public void getHeaders() throws Exception { 
		HttpHeaders headers = response.getHeaders(); 
		String headerName = "MyHeader"; 
		String headerValue1 = "value1"; 
		headers.add(headerName, headerValue1); 
		String headerValue2 = "value2"; 
		headers.add(headerName, headerValue2); 
		headers.setContentType(new MediaType("text", "plain", Charset.forName("UTF-8"))); 
 
		response.close(); 
		assertTrue("Header not set", mockResponse.getHeaderNames().contains(headerName)); 
		List<String> headerValues = mockResponse.getHeaders(headerName); 
		assertTrue("Header not set", headerValues.contains(headerValue1)); 
		assertTrue("Header not set", headerValues.contains(headerValue2)); 
		assertEquals("Invalid Content-Type", "text/plain;charset=UTF-8", mockResponse.getHeader("Content-Type")); 
		assertEquals("Invalid Content-Type", "text/plain;charset=UTF-8", mockResponse.getContentType()); 
		assertEquals("Invalid Content-Type", "UTF-8", mockResponse.getCharacterEncoding()); 
	} 
 
6
CodeRank
This code example shows how to use the following methods:
		verify(sessionCleanupTask).run();
	} 
 
	protected DefaultTransportRequest createTransportRequest(Transport transport, TransportType type) throws Exception {
		SockJsUrlInfo urlInfo = new SockJsUrlInfo(new URI("http://example.com"));
		return new DefaultTransportRequest(urlInfo, new HttpHeaders(), transport, type, CODEC);
	} 
 
} 
6
CodeRank
This code example shows how to use the following methods:
 * @author Rossen Stoyanchev 
 * @since 3.2 
 */ 
public class MockHttpInputMessage implements HttpInputMessage {
 
	private final HttpHeaders headers = new HttpHeaders();
 
	private final InputStream body;
 
 
	public MockHttpInputMessage(byte[] contents) {
		this.body = (contents != null) ? new ByteArrayInputStream(contents) : null;
	} 
 
	public MockHttpInputMessage(InputStream body) {
		Assert.notNull(body, "'body' must not be null");
		this.body = body;
	} 
 
	@Override 
6
CodeRank
This code example shows how to use the following methods:setLocation
		return new ResponseEntity(headers, HttpStatus.CREATED);
	} 
 
	private HttpHeaders createLocation(UriComponentsBuilder uriBuilder, String path) {
		URI uri = uriBuilder.path(path).build().toUri();
		HttpHeaders headers = new HttpHeaders();
		headers.setLocation(uri);
		return headers;
	} 
 
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaTypes.JSON)
	// 按Restful风格约定,返回204状态码, 无内容. 也可以返回200状态码. 
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void update(@RequestBody Task task) {
		counterService.increment("task.update");
		taskService.saveTask(task);
	} 
 
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
6
CodeRank
This code example shows how to use the following methods:add
		String value2 = "baz"; 
 
		request.addHeader(name, value1); 
		request.addHeader(name, value2); 
 
		HttpHeaders expected = new HttpHeaders(); 
		expected.add(name, value1); 
		expected.add(name, value2); 
 
		Object result = resolver.resolveArgument(paramHttpHeaders, null, webRequest, null); 
 
		assertTrue(result instanceof HttpHeaders); 
		assertEquals("Invalid result", expected, result); 
	} 
 
	public void params(@RequestHeader Map<?, ?> param1, 
					   @RequestHeader MultiValueMap<?, ?> param2, 
					   @RequestHeader HttpHeaders param3, 
					   Map<?,?> unsupported) { 
	} 
6
CodeRank
See Code Examples for Spring Framework HttpHeaders Methods: