Category Archives: Code

Containerize Spring Boot App with Docker

  • Add docker maven plugin to pom.xml
<plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.4.0</version>
            <configuration>
                <repository>${docker.image.prefix}/${project.artifactId}</repository>
                <buildArgs>
                    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
            </configuration>
</plugin>
  • Create Dockerfile at the root of the project
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  • Build docker image

mvn clean install dockerfile:build

  • Login to docker hub and push the image to docker hub (Optional Step)

docker login -u <username>
mvn dockerfile:push

  • Verify the image is built and available

docker images

  • Run Spring Boot App from docker

docker run –rm -d –restart -p 8080:8080 ${docker.image.prefix}/${project.artifactId}

Upload MultipartFile using Spring RestTemplate

public boolean uploadAttachment(MultipartFile file) {
        boolean success = false;
        if (!file.isEmpty()) {
            try {
                ByteArrayResource fileAsResource = new ByteArrayResource(file.getBytes()) {
                    @Override
                    public String getFilename() {
                        return file.getOriginalFilename();
                    }
                };
                LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
                map.add("file", fileAsResource);
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.MULTIPART_FORM_DATA);

                HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(map, headers);
                String url = "http://localhost:8084/attachments";

                ResponseEntity<String> response = restTemplate.exchange(url,
                        HttpMethod.POST,
                        entity,
                        String.class);
                if (response.getStatusCode().equals(HttpStatus.OK)) {
                    success = true;
                }
            } catch (Exception e) {
                LOGGER.error("Failed to upload attachment", e);
            }
        }
        return success;
    }

C++ Program To Push To And Pop From Stack

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>

class NODE
{
	public:
		int key;
		NODE * next; 
		NODE(int n);
};

NODE * root;

void push(int n)
{
	NODE *p;
	p=new NODE(n);
	p->next=root;
	root=p;
}

void pop(int & item)
{
	if(root == NULL)
	{ 
		cout<<"Error!!";
		exit(1);
	}
	else
	{
		item=root->key;
		root=root->next;
	}
}

void show()
{
	NODE * q;
	q = root;
	int i=0;
	while(q!=NULL)
	{
		cout<<q->key<<" , ";
		q=q->next;
		if(++i > 10) break;
	}
	cout<<endl;
}

int main()
{
	cout<<"Program stack.cpp starts..."<<endl;
	int i, dataindex, item;
	int Data[10] = {12,14,76,48,10,33,42,98,23,57};
	int Operation[10] = {1,1,1,0,1,0,0,1,0,1};
	root = NULL;
	dataindex = 0;
	for(i=0;i<10;i++)
	{
		if(Operation[i]==1)
		{
			push(Data[dataindex]);
			cout<<"Pushed..."<<Data[dataindex]<<" Stack-> ";
			dataindex++;
			show();
		}
		else
		{
			pop(item);
			cout<<"Popped"<<item<<"Stack-> ";
			show();
		}
	}
	getch();
	return 0;
}

NODE::NODE(int n)
{
	key = n;
	next = NULL;
}

C++ program to perform operations on a text file

#include<fstream.h> 
#include<conio.h>
#include<stdlib.h>

class text_file
{
	char ch;
	char fname[11];
	char fname1[11];
	ifstream ifile;
	ofstream ofile;
public:
	void write()
	{
		cout<<"\nEnter file name(to be created):";
		cin>>ch;
		ofile.open(fname);
		if(!ofile)
		{
			cout<<"\n\n ERROR!!";
			exit(1);
		} 
		cout<<"\nEnter data.....\n(press ESc. to exit editor)";
		while(ch!=(char)27)
		{
			ofile.put(ch);
		}
		ofile.close();
	}
	void read()
	{
		cout<<"\nEnter file name(to be read):";
		cin>>fname;
		ifile.open(fname);
		if(!ifile)
		{
			cout<<"\n\n ERROR!!";
			exit(1);
		} 
		cout<<"\nData is displayed now...";
		while(ifile)
		{
			ifile.get(ch);
			cout<<ch;
		}
		ifile.close();
	}
	void copy()
	{
		cout<<"\nEnter file name(source):";
		cin>>fname;
		ifile.open(fname);
		if(!ifile)
		{
			cout<<"\n\n ERROR!!";
			exit(1);
		}
		cout<<"\nEnter file name(destination):";
		cin>>fname1;
		ofile.open(fname1);
		if(!ofile)
		{
			cout<<"\n\n ERROR!!";
			exit(1);
		} 
		cout<<"\nData is copied";
		while(ifile)
		{
			ifile.get(ch);
			ofile.put(ch);
		}
		ifile.close();
		ofile.close();
	}
	void cut()
	{
		cout<<"\nEnter file name(source):";
		cin>>fname;
		ifile.open(fname);
		if(!ifile)
		{
			cout<<"\n\n ERROR!!";
			exit(1);
		}
		cout<<"\nEnter file name(destination):";
		cin>>fname1;
		ofile.open(fname1);
		if(!ofile)
		{
			cout<<"\n\n ERROR!!";
			exit(1);
		} 
		cout<<"\nData is cut and pasted !!";
		while(ifile)
		{
			ifile.get(ch);
			ofile.put(ch);
		}
		ofile.close();
		ifile.close();
		remove("fname");
	}
};

int main()
{
	int opt;
	text_file t;
	cout<<"\n1. Create";
	cout<<"\n2. Read";
	cout<<"\n3. Copy";
	cout<<"\n4. Cut";
	cout<<"\nEnter option";
	cin>>opt;
	switch(opt)
	{
		case 1: t.create();
			break;
		case 2:	t.read();
			break;
		case 3:	t.copy();
			break;
		case 4:	t.cut();
			break;
		default: cout<<"\nWrong option!!";
	}
	return 0;
}