Skip to content

Instantly share code, notes, and snippets.

@codinko
codinko / deleteNoteUsageOfJavaStream.java
Last active November 18, 2018 23:46
delete a note using java stream
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 November 13, 2018 03:34
JWT usage in Rest Controller while accessing resource passing the existing JWT
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 November 13, 2018 03:02
JWT creation in Rest Controller after validating user
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 04:52
application.properties for Spring hello boot
server.port=8889
hello.greeting= nice to meet you
hello.version= 2.0
@codinko
codinko / HelloController.java
Created May 10, 2017 04:51
HelloController for Spring hello boot
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 04:49
manifest.yml file for hello-boot Spring boot app
---
applications:
- name: hello-boot
instances: 1
host: hello-boot-${random-word}
path: target/hello-boot-1.0.jar
// 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 March 1, 2016 17:54
Spring REST API - Integrating Swagger
/* 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>
@codinko
codinko / APIContract.java
Created February 29, 2016 03:09
Spring REST API
package com.codinko.springrestapi.vo;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
// Generic class that can have data of generic type. See it's usage when an instance of this class is created
public class APIContract<T> {
private boolean success;
private String message;
@codinko
codinko / samplePOMwithnothing.xml
Created February 27, 2016 23:49
pom.xml created by eclipse, before we're adding any dependencies or plugins
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codinko.restapi</groupId>
<artifactId>restapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>