Package cz.muni.fi.pa165.ddtroopsconsole.service

Source Code of cz.muni.fi.pa165.ddtroopsconsole.service.SkillServiceImpl

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.muni.fi.pa165.ddtroopsconsole.service;

import cz.muni.fi.pa165.ddtroops.dto.SkillDTO;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.SkillService;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.client.RestTemplate;

/**
*
* @author Mattysek
*/
public class SkillServiceImpl implements SkillService
{

    private RestTemplate rt;
    private String url;
    public SkillServiceImpl(String url)
    {
        this.url = url;
        SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() {
        @Override
        protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        super.prepareConnection(connection, httpMethod);

        //Basic Authentication for Police API
        String authorisation = "rest" + ":" + "rest";
        byte[] encodedAuthorisation = Base64.encode(authorisation.getBytes());
        connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation));
        }
        };
        rt = new RestTemplate(s);
        List<HttpMessageConverter<?>> l = new ArrayList<>();
        l.add(new MappingJacksonHttpMessageConverter());
        rt.setMessageConverters(l);
    }
   
    @Override
    public SkillDTO createNewSkill(String name, String description, String profession, Long minXP) {
        SkillDTO skill = new SkillDTO();
        skill.name = name;
        skill.description = description;
        skill.profession = profession;
        skill.minXP = minXP;
        return skill;
    }

    @Override
    public void create(SkillDTO skill)
    {
        if (rt.postForLocation(url, skill) == null) {
            System.out.println("Can't create invalid skill.");
            System.exit(1);
        }
        else {
            System.out.println("New skill was created.");
        }
    }

    @Override
    public void update(SkillDTO skill) {
        if(getById(skill.getId()) != null)
        {
            rt.put(url+"{id}", skill, skill.getId());
            System.out.println("Skill was updated.");
        }
        else {
            System.out.println("Skill with given id (" + skill.getId() + ") does not exist.");
            System.exit(1);
        }
    }

    @Override
    public void delete(SkillDTO skill) {
        if(getById(skill.getId()) != null)
        {
            rt.delete(url+"{id}", skill.getId());
            System.out.println("Skill was deleted.");
        }
        else {
            System.out.println("Skill with given id (" + skill.getId() + ") does not exist.");
            System.exit(1);
        }
    }

    @Override
    public SkillDTO getById(Long id) {
        return rt.getForObject(url+"{id}",SkillDTO.class,id);
    }

    @Override
    public List<SkillDTO> getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<SkillDTO> getByProfession(String profession) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<SkillDTO> getByXp(Long xp) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<SkillDTO> getByProfessionAndXp(String profession, Long xp) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
   
}
TOP

Related Classes of cz.muni.fi.pa165.ddtroopsconsole.service.SkillServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.