8 Oct 2017

HTTP content type headers for different purpose in Rest API

Rest API  to return a zip file as download

@RequestMapping(value="/v1/collection_service/response/{appReferenceId}", method=RequestMethod.GET, produces=MediaType.MULTIPART_FORM_DATA_VALUE)

 public ResponseEntity<?> collectionServiceProfile(@PathVariable(value="appReferenceId") String appReferenceId){
  ByteBuffer byteBuffer = collectionService.getCollectionProfileZip(appReferenceId);
  MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
  headers.add(HttpHeaders.CONTENT_TYPE, "application/zip");
  return new ResponseEntity<>(byteBuffer.array(), headers, HttpStatus.OK);
 }

The line in the block is the one that determines that the content should be downloaded as zip.

6 Oct 2017

Store Zip file as blob in Cassandra using Spring data

In this blog we are going to see how we will save a zip file into Cassandra database table column. 

 For this, let's consider an example of Employee table where we want to save employee resume as a zip file into the table. The table looks like below

employee_id(int)
employee_name(text)
employee_type(text)
employee_resume(blob)











The Employee entity class would look like below

@Table(value = "employee")
public class EmployeeBean {

@Column(value = "employee_id")
private Integer employeeId;

@Column(value = "employee_name")
private String employeeName;

@Column(value = "employee_type")
private String employeeType;

@Column(value = "employee_resume")
private ByteBuffer employeeResume;

public Integer getEmployeeId() {
return employeeId;
}

public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}

public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public String getEmployeeType() {
return employeeType;
}

public void setEmployeeType(String employeeType) {
this.employeeType = employeeType;
}

public ByteBuffer getEmployeeResume() {
return employeeResume;
}

public void setEmployeeResume(ByteBuffer employeeResume) {
this.employeeResume = employeeResume;
}

}


The EmployeeService would look like the below for saving an entry in to the table "employee"

public class EmployeeService {

private CassandraOperations cassandraTemplate;
public void saveEmployee(Integer id, String name, String type, String strResume) {
EmployeeBean emp = new EmployeeBean();
emp.setEmployeeId(id);
emp.setEmployeeName(name);
emp.setEmployeeType(type);
ByteBuffer resumeByteBuffer = ByteBuffer.wrap(strResume.getBytes());
emp.setEmployeeResume(resumeByteBuffer);
cassandraTemplate.insert(emp);
}

}

Please note the Java type for blob in Cassandra is ByteBuffer(for column resume in employee table). 
Convert the String resume to byte array and wrap into ByteBuffer and save into the table.