/*
Copyright 2015 Cleuton Sampaio de Melo Jr

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package com.obomprogramador.dropbackend;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.codahale.metrics.annotation.Timed;

@Path("/login")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class RestResource {
	private String dbUrl;
	private DrbDao dao;
	
	public RestResource(String dbUrl) {
		this.dbUrl = dbUrl;
		this.dao = new DrbDao(dbUrl);
	}
	
	@POST
	@Timed
	public Response processaLogin(LoginCredential login) {
		int httpStatus = 200;
		NewsFeed nf = null;
		if (dao.getLogin(login)) {
			nf = getNewsFeed();
		}
		else {
			nf = new NewsFeed();
			nf.setStatus(NewsFeed.FAIL);
			nf.setMessage("User not authenticated");
			nf.setData(new ArrayList<NewsLine>());
			httpStatus = 403;
		}
		return Response.status(httpStatus).entity(nf.toString()).build();
	}

	private NewsFeed getNewsFeed() {
		NewsFeed nf = new NewsFeed();
		nf.setStatus(NewsFeed.SUCCESS);
		nf.setMessage("OK");
		nf.setData(dao.getNewsFeed());
		return nf;
	}
}

/*
Copyright 2015 Cleuton Sampaio de Melo Jr

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package com.obomprogramador.dropbackend;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.codahale.metrics.annotation.Timed;

@Path("/login")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class RestResource {
	private String dbUrl;
	private DrbDao dao;
	
	public RestResource(String dbUrl) {
		this.dbUrl = dbUrl;
		this.dao = new DrbDao(dbUrl);
	}
	
	/* ------------ ATTENTION ---------------------------------
		This method should be protected. You should create 
		an "Authenticator class" and protect the method 
		with the "@Auth" annotation. 
		Also, after login, you should return some kind of
		security token to Client side, as it must include an
		"Authorization" header, with that token, in each 
		request.
		
		See: https://dropwizard.github.io/dropwizard/0.6.2/manual/auth.html
	*/
	@POST
	@Timed
	public Response processaLogin(LoginCredential login) {
		int httpStatus = 200;
		NewsFeed nf = null;
		if (dao.getLogin(login)) {
			nf = getNewsFeed();
		}
		else {
			nf = new NewsFeed();
			nf.setStatus(NewsFeed.FAIL);
			nf.setMessage("User not authenticated");
			nf.setData(new ArrayList<NewsLine>());
			httpStatus = 403;
		}
		return Response.status(httpStatus).entity(nf.toString()).build();
	}

	private NewsFeed getNewsFeed() {
		NewsFeed nf = new NewsFeed();
		nf.setStatus(NewsFeed.SUCCESS);
		nf.setMessage("OK");
		nf.setData(dao.getNewsFeed());
		return nf;
	}
}

Add comment