Pionads Helpfiles: Java Rest Client

Home

Java Rest Client

Java REST Client

package apps.bapp.logic.publishing;

import core.helpers.*;
import core.logic.http.RawHttpGet;
import core.logic.logging.Logger;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.httpclient.URIException;
import org.apache.http.HttpResponse;
import org.w3c.dom.Document;

import javax.servlet.ServletOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Created by Marius van Zwijndregt (c) Pionic
 * User: Marius
 * Date: 18-9-2014
 * Time: 20:52
 */
@Getter
@Setter
public class PionAdsRestAdapter
{

	public static PionAdsRestAdapter pionAdsRestAdapter = null;

	private String adId;
	private String content;
	private String contentParsed;
	private String adspaceId;
	private String linkUrl;
	private String publishId;
	private String impressionId;
	private String imgSrc;
	private String imgBase64;
	private String instanceId;

	public static PionAdsRestAdapter fetchAdspace(String rootUrl, String adspaceId, String key, String instanceId) throws UnsupportedEncodingException, URIException, MalformedURLException, URISyntaxException
	{
		PionAdsRestAdapter result = new PionAdsRestAdapter();

		BaseHTTP baseHTTP = new BaseHTTP(150);

		String restUrl = rootUrl;
		if (!restUrl.endsWith("/"))
		{
			restUrl += "/";
		}
		restUrl += "rest/publish-adspace/" + adspaceId + "?key=" + key + "&clientHash=" + BaseServlet.getIP();

		URL url = BaseURL.newURL(restUrl);

		RawHttpGet httpGet = baseHTTP.createRawHttpGet(url.toURI());
		httpGet.addHeader("User-Agent", "PionAds Java client 0.5b");
		httpGet.addHeader("X-User-Agent", BaseServlet.getUserAgent());
		httpGet.addHeader("X-Real-IP", BaseServlet.getIP());
		httpGet.addHeader("Accept", "application/xml");
		httpGet.addHeader("Cache-Control", "no-cache");

		HttpResponse response = null;
		String content = "";

		try
		{

			content = baseHTTP.downloadToStringWithURLConnection(url);

			// MZ: Decrypt the response
			content = BaseString.replaceAllStatic("€", "", content);
			content = content.trim();
			Document xmlDoc = BaseXML.documentFromString(content);
			result.setImpressionId(BaseXML.xpathSingle("/root/ad/impressionid", xmlDoc));
			result.setPublishId(BaseXML.xpathSingle("/root/ad/impressionid", xmlDoc));
			result.setImgBase64(BaseXML.xpathSingle("/root/ad/img/srcbase64", xmlDoc));
			result.setImgSrc(BaseXML.xpathSingle("/root/ad/img/src", xmlDoc));
			result.setContent(BaseXML.xpathSingle("/root/ad/content", xmlDoc));
			result.setContentParsed(BaseXML.xpathSingle("/root/ad/contentparsed", xmlDoc));
			result.setAdId(BaseXML.xpathSingle("/root/ad/id", xmlDoc));
			result.setLinkUrl(BaseXML.xpathSingle("/root/ad/clickurl", xmlDoc));
			result.setInstanceId(instanceId);
			result.setAdspaceId(adspaceId);


		}
		catch (Exception e)
		{
			Logger.error("Could not load url: " + e.getMessage(), e);
		}

		return result;

	}

	public static PionAdsRestAdapter callClick(String rootUrl, String adspaceId, String key, String adId) throws UnsupportedEncodingException, URIException, MalformedURLException, URISyntaxException
	{
		PionAdsRestAdapter result = new PionAdsRestAdapter();

		BaseHTTP baseHTTP = new BaseHTTP();

		String restUrl = rootUrl;
		if (!restUrl.endsWith("/"))
		{
			restUrl += "/";
		}
		restUrl += "rest/click-adspace/" + adspaceId + "/" + adId + "?key=" + key + "&clientHash=" + BaseServlet.getIP();

		URL url = BaseURL.newURL(restUrl);

		RawHttpGet httpGet = baseHTTP.createRawHttpGet(url.toURI());
		httpGet.addHeader("User-Agent", "PionAds Java client 0.5b");
		httpGet.addHeader("Accept", "application/xml");
		httpGet.addHeader("Cache-Control", "no-cache");

		HttpResponse response = null;
		String content = "";

		try
		{

			content = baseHTTP.downloadToStringWithURLConnection(url);

			// MZ: Decrypt the response
			content = BaseString.replaceAllStatic("€", "", content);
			content = content.trim();
			Document xmlDoc = BaseXML.documentFromString(content);
			result.setLinkUrl(BaseXML.xpathSingle("/root/ad/deeplink", xmlDoc));

		}
		catch (Exception e)
		{
			Logger.error("Could not load url: " + e.getMessage(), e);
		}

		return result;

	}


	public static void serveImage(String rootUrl, String key, String adId, ServletOutputStream outputStream) throws UnsupportedEncodingException, URIException, MalformedURLException, URISyntaxException
	{
		BaseHTTP baseHTTP = new BaseHTTP();

		String restUrl = rootUrl;
		if (!restUrl.endsWith("/"))
		{
			restUrl += "/";
		}
		restUrl += "rest/fetch-image/" + adId + "?key=" + key + "&clientHash=" + BaseServlet.getIP();

		URL url = BaseURL.newURL(restUrl);

		RawHttpGet httpGet = baseHTTP.createRawHttpGet(url.toURI());
		httpGet.addHeader("User-Agent", "PionAds Java client 0.5b");
		httpGet.addHeader("Accept", "application/xml");
		httpGet.addHeader("Cache-Control", "no-cache");


		BaseURL.storeToStream(url, outputStream);

	}
}
[Please implement an object class template for: core.managers.ResourceManager/resourcemanager_js]