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

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

/*
* 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.RaceDTO;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.RaceService;
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.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 RaceServiceImpl implements RaceService
{
    private RestTemplate rt;
    private String url;
    public RaceServiceImpl(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 RaceDTO createNewRace(String name, String description, Byte strength, Byte dexterity, Byte constitution, Byte intelligence, Byte wisdom, Byte charisma) {
        RaceDTO race = new RaceDTO();
        race.charisma = charisma;
        race.constitution = constitution;
        race.description = description;
        race.dexterity = dexterity;
        race.intelligence = intelligence;
        race.name = name;
        race.strength = strength;
        race.wisdom = wisdom;
        return race;
    }

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

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

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

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

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

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

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.