~smaioli/azureus/ubuntu-experimental

« back to all changes in this revision

Viewing changes to com/aelitis/azureus/util/ContentNetworkUtils.java

MergedĀ VuzeĀ 4.2.0.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Created on Dec 10, 2008
 
3
 *
 
4
 * Copyright 2008 Vuze, Inc.  All rights reserved.
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; version 2 of the License only.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA 
 
17
 */
 
18
 
 
19
package com.aelitis.azureus.util;
 
20
 
 
21
import java.net.MalformedURLException;
 
22
import java.net.URL;
 
23
 
 
24
import com.aelitis.azureus.core.cnetwork.ContentNetwork;
 
25
import com.aelitis.azureus.core.cnetwork.ContentNetworkManagerFactory;
 
26
 
 
27
/**
 
28
 * @author TuxPaper
 
29
 * @created Dec 10, 2008
 
30
 *
 
31
 */
 
32
public class ContentNetworkUtils
 
33
{
 
34
 
 
35
        /**
 
36
         * Get content network url based on service id.
 
37
         * @param cn
 
38
         * @param serviceID
 
39
         * @return null if service is not supported
 
40
         *
 
41
         * @since 4.0.0.5
 
42
         */
 
43
        public static String getUrl(ContentNetwork cn, int serviceID) {
 
44
                try {
 
45
                        if (!cn.isServiceSupported(serviceID)) {
 
46
                                return null;
 
47
                        }
 
48
                        return cn.getServiceURL(serviceID);
 
49
                } catch (Throwable t) {
 
50
                        return null;
 
51
                }
 
52
 
 
53
        }
 
54
        public static String getUrl(ContentNetwork cn, int serviceID, Object[] params) {
 
55
                try {
 
56
                        if (!cn.isServiceSupported(serviceID)) {
 
57
                                return null;
 
58
                        }
 
59
                        return cn.getServiceURL(serviceID, params);
 
60
                } catch (Throwable t) {
 
61
                        return null;
 
62
                }
 
63
        }
 
64
 
 
65
        public static ContentNetwork getContentNetworkFromTarget(String target) {
 
66
                ContentNetwork cn = null;
 
67
                if (target != null && target.startsWith("ContentNetwork.")) {
 
68
                        long networkID = Long.parseLong(target.substring(15));
 
69
                        cn = ContentNetworkManagerFactory.getSingleton().getContentNetwork(
 
70
                                        networkID);
 
71
                }
 
72
 
 
73
                if (cn == null) {
 
74
                        cn = ConstantsVuze.getDefaultContentNetwork();
 
75
                }
 
76
                return cn;
 
77
        }
 
78
 
 
79
        public static String getTarget(ContentNetwork cn) {
 
80
                return "ContentNetwork."
 
81
                                + (cn == null ? ConstantsVuze.getDefaultContentNetwork().getID()
 
82
                                                : cn.getID());
 
83
        }
 
84
 
 
85
        public static void setSourceRef(String target, String sourceRef,
 
86
                        boolean override) {
 
87
                setSourceRef(getContentNetworkFromTarget(target), sourceRef, override);
 
88
        }
 
89
 
 
90
        public static void setSourceRef(ContentNetwork cn, String sourceRef,
 
91
                        boolean override) {
 
92
                if (cn == ConstantsVuze.getDefaultContentNetwork()) {
 
93
                        return;
 
94
                }
 
95
                // always override old source ref if the content network requires
 
96
                // authorization and the user hasn't authorized yet.
 
97
                if (cn.isServiceSupported(ContentNetwork.SERVICE_AUTHORIZE)) {
 
98
                        boolean authShown = false; 
 
99
                        Object oAuthShown = cn.getPersistentProperty(ContentNetwork.PP_AUTH_PAGE_SHOWN);
 
100
                        if (oAuthShown instanceof Boolean) {
 
101
                                authShown = ((Boolean) oAuthShown).booleanValue();
 
102
                        }
 
103
                        if (!authShown) {
 
104
                                override = true;
 
105
                        }
 
106
                }
 
107
                
 
108
                String old = (String) cn.getPersistentProperty(ContentNetwork.PP_SOURCE_REF);
 
109
                if (old == null || override) {
 
110
                        if (sourceRef != null && sourceRef.startsWith("http")) {
 
111
                                // trim down
 
112
                                try {
 
113
                                        URL url = new URL(sourceRef);
 
114
                                        sourceRef = url.getHost() + url.getPath();
 
115
                                } catch (MalformedURLException e) {
 
116
                                }
 
117
                        }
 
118
                        cn.setPersistentProperty(ContentNetwork.PP_SOURCE_REF, sourceRef);
 
119
                }
 
120
        }
 
121
}