Guest User

test

a guest
Sep 29th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import javax.servlet.http.HttpServletRequest;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.http.HttpEntity;
  6. import org.springframework.http.HttpHeaders;
  7. import org.springframework.http.HttpMethod;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.RequestBody;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestMethod;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import org.springframework.web.client.RestTemplate;
  15. @RestController
  16. public class AddContactController {
  17.     private static final Logger log = LoggerFactory.getLogger(ListController.class);
  18.    
  19.     @RequestMapping(value="/add", method=RequestMethod.POST)
  20.     public String addContact(HttpServletRequest request, @RequestBody Contact contact) {
  21.        
  22.         String url = Constants.CONTACTS_ADD_URL;
  23.         String username = request.getHeader("auth-user");
  24.         String password = request.getHeader("auth-password");
  25.        
  26.         HttpHeaders headers = new HttpHeaders();
  27.         if (username != null && password != null){
  28.             CreateBasicAuth creds = new CreateBasicAuth(username, password);
  29.             headers.set("Authorization", "Basic  "+ creds.base64creds());
  30.         }
  31.         headers.setContentType(MediaType.APPLICATION_JSON);
  32.         headers.set("Accept", "application/x-spring-data-verbose+json");
  33.        
  34.         HttpEntity<Contact> entity = new HttpEntity<Contact>(contact,headers);
  35.         RestTemplate restTemplate = new RestTemplate();
  36.        
  37.         try {
  38.             ResponseEntity<Contacts> response = restTemplate.exchange(url,
  39.                 HttpMethod.POST, entity, Contacts.class);
  40.             return "Status: " + response.getStatusCodeValue();
  41.         } catch (Exception e)
  42.         {
  43.             log.info(e.getMessage());
  44.             return "Error while processing your request";
  45.         }
  46.     }
  47. }
Add Comment
Please, Sign In to add comment