Skip to content

Instantly share code, notes, and snippets.

codinko codinko

View GitHub Profile
@codinko
codinko / java.util.Optional.map.codesnippet1.java
Last active Nov 19, 2018
java.util.Optional.map code snippet-1
View java.util.Optional.map.codesnippet1.java
// Get all notes by userId
java.util.Optional;
java.util.Optional.map()
java.util.Optional class
// A container object which may or may not contain a non-null value.
// If a value is present, {@code isPresent()} will return {@code true} and
// {@code get()} will return the value.
// Approach-1
View NoteUserJavaStreamUsageWithMapReduce.java
// How to get a Note from noteId and userId
private Note findNote(int noteId, List<Note> notes) {
return notes.stream()
.filter(note -> note.getNoteId() == noteId)
.findAny()
.orElse(null);
}
// Approach-1
@codinko
codinko / deleteNoteUsageOfJavaStream.java
Last active Nov 18, 2018
delete a note using java stream
View deleteNoteUsageOfJavaStream.java
public boolean deleteNote(String userId, int noteId) {
NoteUser dbUser = this.noteRepository.findById(userId).orElse(null);
Note dbNote = null;
if(dbUser != null) {
dbNote = this.findNote(noteId, dbUser.getNotes());
if(dbNote != null) {
dbUser.getNotes().remove(dbNote);
dbUser = this.noteRepository.save(dbUser);
}
@codinko
codinko / CodeParsingJWT_ReceivedFromRequest.java
Created Nov 13, 2018
JWT usage in Rest Controller while accessing resource passing the existing JWT
View CodeParsingJWT_ReceivedFromRequest.java
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
final HttpServletRequest req = (HttpServletRequest) request;
final String authHeader = req.getHeader("authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
throw new ServletException("Missing or invalid Authorization header");
}
@codinko
codinko / AuthenticationControllerCreatingJWT.java
Last active Nov 13, 2018
JWT creation in Rest Controller after validating user
View AuthenticationControllerCreatingJWT.java
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
@RestController
public class AuthenticationController { ...
@ApiOperation(value = "Validate the user login")
@PostMapping("/api/authentication/login")
public ResponseEntity<?> userLogin(@RequestBody User user) {
String jwtToken = "";
@codinko
codinko / application.properties
Created May 10, 2017
application.properties for Spring hello boot
View application.properties
server.port=8889
hello.greeting= nice to meet you
hello.version= 2.0
@codinko
codinko / HelloController.java
Created May 10, 2017
HelloController for Spring hello boot
View HelloController.java
package demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@codinko
codinko / manifest.yml
Created May 10, 2017
manifest.yml file for hello-boot Spring boot app
View manifest.yml
---
applications:
- name: hello-boot
instances: 1
host: hello-boot-${random-word}
path: target/hello-boot-1.0.jar
View simple-inefficient-quicksort-algo
// Sort an array using a simple but inefficient quicksort.
public int[] quicksortSimple(int[] data) {
if (data.length < 2) {
return data;
}
int pivotIndex = data.length / 2;
int pivotValue = data[pivotIndex];
int leftCount = 0;
// Count how many are less than the pivot
@codinko
codinko / SwaggerChangesForSpringREST.java
Created Mar 1, 2016
Spring REST API - Integrating Swagger
View SwaggerChangesForSpringREST.java
/* Mentioning all the swagger changes made on top of
Spring REST app: https://gist.github.com/codinko/79986dbed38d0f2b2d1b */
---------------------------------------
Modified File: pom.xml
---------------------------------------
<properties>
<swagger.version>1.0.2</swagger.version>
</properties>
<dependencies>
<dependency>
You can’t perform that action at this time.