<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package se.malmo.www.kontaktruta.service;

import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.KeyGenerator;
import com.googlecode.ehcache.annotations.PartialCacheKey;
import com.googlecode.ehcache.annotations.Property;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import se.malmo.www.kontaktruta.model.APISuggestion;

/**
 *
 * @author rbolton
 */
@Component
public class ContactServiceImpl implements ContactService {
    
    private RestTemplate restTemplate;
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate){
        HostnameVerifier verifier = new NullHostnameVerifier();
        AppSimpleClientHttpRequestFactory factory = new AppSimpleClientHttpRequestFactory(verifier);
        restTemplate.setRequestFactory(factory);
        this.restTemplate = restTemplate;
    }
    
    @Value("${contact.api.baseurl}")
    private String BASEURL;
    
    @Override
    @Cacheable(cacheName="contacts", 
        cacheNull = false,
        keyGenerator = @KeyGenerator (
            name = "StringCacheKeyGenerator",
            properties = {
                    @Property( name="includeMethod", value="false" ),
                    @Property( name="includeParameterTypes", value="false" )
                    
            }
        )
    )
    public <T extends APISuggestion> T getContact(@PartialCacheKey String contactType, @PartialCacheKey String id, Class<T> classType) {
        T contact = null;
        try {
            contact = restTemplate.getForObject(String.format(BASEURL, contactType, id).replace("&app_token", "?app_token"), classType);
            Logger.getLogger(ContactServiceImpl.class.getName()).log(Level.INFO, new StringBuilder(" found: ").append(contact.getName()).append(" for id:").append(id).toString());
        } catch (HttpClientErrorException ex){
            Logger.getLogger(ContactServiceImpl.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
            return contact;
        }   
        return contact;
    }

    @Override
    public <T extends APISuggestion> T getContactByName(String contactType, String name, Class<T> classType) {       
        APISuggestion[] contacts = getContactList(contactType, name, APISuggestion[].class);
        if (contacts != null && contacts.length > 0){
            String id = contacts[0].getId();
            return getContact(contactType, id, classType);
        }
        return null;
    }

    @Override
    public <T> T getContactList(String contactType, String query, Class<T> type) {
        query = "search?q=" + query;
        T contacts = null;
        try {
            contacts = restTemplate.getForObject(String.format(BASEURL, contactType, query), type);
        } catch (HttpClientErrorException ex){
            Logger.getLogger(ContactServiceImpl.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);<commit_msg>Fånga 404 fel och ge kort felmeddealande istället för exception. Övriga fel, icke förväntade, kastas än i loggen.<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package se.malmo.www.kontaktruta.service;

import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.KeyGenerator;
import com.googlecode.ehcache.annotations.PartialCacheKey;
import com.googlecode.ehcache.annotations.Property;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import se.malmo.www.kontaktruta.model.APISuggestion;

/**
 *
 * @author rbolton
 */
@Component
public class ContactServiceImpl implements ContactService {
    
    private RestTemplate restTemplate;
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate){
        HostnameVerifier verifier = new NullHostnameVerifier();
        AppSimpleClientHttpRequestFactory factory = new AppSimpleClientHttpRequestFactory(verifier);
        restTemplate.setRequestFactory(factory);
        this.restTemplate = restTemplate;
    }
    
    @Value("${contact.api.baseurl}")
    private String BASEURL;
    
    @Override
    @Cacheable(cacheName="contacts", 
        cacheNull = false,
        keyGenerator = @KeyGenerator (
            name = "StringCacheKeyGenerator",
            properties = {
                    @Property( name="includeMethod", value="false" ),
                    @Property( name="includeParameterTypes", value="false" )
                    
            }
        )
    )
    public <T extends APISuggestion> T getContact(@PartialCacheKey String contactType, @PartialCacheKey String id, Class<T> classType) {
        T contact = null;
        try {
            contact = restTemplate.getForObject(String.format(BASEURL, contactType, id).replace("&app_token", "?app_token"), classType);
            Logger.getLogger(ContactServiceImpl.class.getName()).log(Level.INFO, new StringBuilder(" found: ").append(contact.getName()).append(" for id:").append(id).toString());
        } catch (HttpClientErrorException ex){
            if(ex.getMessage().equals("404 Not Found")){
                Logger.getLogger(ContactServiceImpl.class.getName()).log(Level.WARNING, ex.getMessage() + " Contact type: " + contactType + " Id: " + id );
            }else
                Logger.getLogger(ContactServiceImpl.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
            return contact;
        }
        return contact;
    }

    @Override
    public <T extends APISuggestion> T getContactByName(String contactType, String name, Class<T> classType) {       
        APISuggestion[] contacts = getContactList(contactType, name, APISuggestion[].class);
        if (contacts != null && contacts.length > 0){
            String id = contacts[0].getId();
            return getContact(contactType, id, classType);
        }
        return null;
    }

    @Override
    public <T> T getContactList(String contactType, String query, Class<T> type) {
        query = "search?q=" + query;
        T contacts = null;
        try {
            contacts = restTemplate.getForObject(String.format(BASEURL, contactType, query), type);
        } catch (HttpClientErrorException ex){
            if(ex.getMessage().equals("404 Not Found")){
                Logger.getLogger(ContactServiceImpl.class.getName()).log(Level.SEVERE, ex.getMessage() + " Contact type: " + contactType + " Query: " + query );
            }else
                Logger.getLogger(ContactServiceImpl.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
{'fraction_non_alphanumeric': 0.1078536847767617, 'fraction_numerical': 0.0017482517482517483, 'mean_word_length': 3.242441528807758, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6721768', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1987, 'n_words': 438}