/*
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;
	}
}

@@ -1149,16 +1149,461 @@
 );%0A%09%7D%0A%09%0A
+%09/* ------------ ATTENTION ---------------------------------%0A%09%09This method should be protected. You should create %0A%09%09an %22Authenticator class%22 and protect the method %0A%09%09with the %22@Auth%22 annotation. %0A%09%09Also, after login, you should return some kind of%0A%09%09security token to Client side, as it must include an%0A%09%09%22Authorization%22 header, with that token, in each %0A%09%09request.%0A%09%09%0A%09%09See: https://dropwizard.github.io/dropwizard/0.6.2/manual/auth.html%0A%09*/%0A
 %09@POST%0A%09

Add comment