Skip to content

Instantly share code, notes, and snippets.

View joshlong's full-sized avatar
🍃
i'm on Twitter @starbuxman

Josh Long joshlong

🍃
i'm on Twitter @starbuxman
View GitHub Profile
// ...
registry.addInterceptor(new LocaleChangeInterceptor());
// ...
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.ENGLISH);
return sessionLocaleResolver;
}
@joshlong
joshlong / gist:5129983
Created March 10, 2013 19:21
getUserById
@RequestMapping(value = USER_COLLECTION_ENTRY_URL, method = RequestMethod.GET)
@ResponseBody
public User getUserById(@PathVariable("userId") Long userId) {
return this.userService.getUserById(userId);
}
@joshlong
joshlong / gist:5130002
Created March 10, 2013 19:26
fields for URLs
static public final String USER_COLLECTION_URL = "/api/users";
static public final String USER_COLLECTION_ENTRY_URL = USER_COLLECTION_URL + "/{userId}";
@joshlong
joshlong / gist:5154456
Created March 13, 2013 17:45
JSON content negotiation
{
"id":58,
"customers":null,
"importedFromServiceProvider":true,
"firstName":"Josh",
"lastName":"Long",
"username":"josh@joshlong.com",
"password":"myPassword",
"profilePhotoImported":true,
"profilePhotoExt":null,
@joshlong
joshlong / gist:5154481
Created March 13, 2013 17:49
content negotiation in XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user
username="josh@joshlong.com"
signupDate="2013-02-21T23:38:30.936-08:00"
profilePhotoImported="true"
password="myPassword"
lastName="Long"
importedFromServiceProvider="true"
id="58"
firstName="Josh"
@RequestMapping(value = USER_COLLECTION_ENTRY_PHOTO_URL, method = RequestMethod.POST)
@ResponseBody
public Long uploadBasedOnPathVariable(@PathVariable("userId") Long userId, @RequestParam("file") MultipartFile file) throws Throwable {
// ...
}
@RequestMapping(value = USER_COLLECTION_ENTRY_PHOTO_URL, method = RequestMethod.GET)
public void renderMedia(HttpServletResponse httpServletResponse, OutputStream os, @PathVariable("userId") Long userId) throws Throwable {
@RequestMapping(value = USER_COLLECTION_ENTRY_PHOTO_URL, method = RequestMethod.POST)
@ResponseBody
public Long uploadBasedOnPathVariable(@PathVariable("userId") Long userId, @RequestParam("file") MultipartFile file) throws Throwable {
byte[] bytesForImage = file.getBytes();
userService.writeUserProfilePhotoAndQueueForConversion(userId, file.getName(), bytesForImage);
return userId;
}
@RequestMapping(value = USER_COLLECTION_ENTRY_PHOTO_URL, method = RequestMethod.GET)
public void renderMedia(HttpServletResponse httpServletResponse, OutputStream os, @PathVariable("userId") Long userId) throws Throwable {
@joshlong
joshlong / gist:5154769
Created March 13, 2013 18:25
how to properly send a header and a status code
@RequestMapping(value = USER_COLLECTION_URL, method = RequestMethod.POST)
public ResponseEntity<User> registerUser(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("firstname") String fn,
@RequestParam("lastname") String ln,
@RequestParam("imported") boolean importedFromServiceProvider,
UriComponentsBuilder componentsBuilder) throws Throwable {
User user = this.userService.createOrGet(username, password, fn, ln, importedFromServiceProvider);
@joshlong
joshlong / gist:5156244
Created March 13, 2013 21:09
write a user's profile photo
@RequestMapping(value = USER_COLLECTION_ENTRY_PHOTO_URL, method = RequestMethod.POST)
@ResponseBody
public Long uploadBasedOnPathVariable(@PathVariable("userId") Long userId, @RequestParam("file") MultipartFile file) throws Throwable {
byte[] bytesForImage = file.getBytes();
userService.writeUserProfilePhotoAndQueueForConversion(userId, file.getName(), bytesForImage);
return userId;
}
@joshlong
joshlong / gist:5156263
Last active December 14, 2015 22:09
reading a user's profile photo
@RequestMapping(value = USER_COLLECTION_ENTRY_PHOTO_URL, method = RequestMethod.GET)
public ResponseEntity<byte[]> renderMedia(@PathVariable("userId") Long userId) throws Throwable {
InputStream is = userService.readUserProfilePhoto(userId);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.IMAGE_JPEG);
byte buffer[] = IOUtils.toByteArray(is);
return new ResponseEntity<byte[]>(buffer, httpHeaders, HttpStatus.OK);
}