Search in sources :

Example 56 with URISyntaxException

use of java.net.URISyntaxException in project okhttp by square.

the class URLEncodingTest method backdoorUrlToUri.

private URI backdoorUrlToUri(URL url) throws Exception {
    final AtomicReference<URI> uriReference = new AtomicReference<>();
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    Internal.instance.setCache(builder, new InternalCache() {

        @Override
        public Response get(Request request) throws IOException {
            uriReference.set(request.url().uri());
            throw new UnsupportedOperationException();
        }

        @Override
        public CacheRequest put(Response response) throws IOException {
            return null;
        }

        @Override
        public void remove(Request request) throws IOException {
        }

        @Override
        public void update(Response cached, Response network) {
        }

        @Override
        public void trackConditionalCacheHit() {
        }

        @Override
        public void trackResponse(CacheStrategy cacheStrategy) {
        }
    });
    try {
        HttpURLConnection connection = new OkUrlFactory(builder.build()).open(url);
        connection.getResponseCode();
    } catch (Exception expected) {
        if (expected.getCause() instanceof URISyntaxException) {
            expected.printStackTrace();
        }
    }
    return uriReference.get();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) CacheRequest(okhttp3.internal.cache.CacheRequest) InternalCache(okhttp3.internal.cache.InternalCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Response(okhttp3.Response) OkUrlFactory(okhttp3.OkUrlFactory) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(okhttp3.internal.cache.CacheRequest) CacheStrategy(okhttp3.internal.cache.CacheStrategy)

Example 57 with URISyntaxException

use of java.net.URISyntaxException in project flink by apache.

the class FilesystemSchemeConfigTest method testSettingFilesystemScheme.

private void testSettingFilesystemScheme(boolean useDefaultScheme, String configFileScheme, boolean useExplicitScheme) {
    final File tmpDir = getTmpDir();
    final File confFile = new File(tmpDir, GlobalConfiguration.FLINK_CONF_FILENAME);
    try {
        confFile.createNewFile();
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create file", e);
    }
    final File testFile = new File(tmpDir.getAbsolutePath() + File.separator + "testing.txt");
    try {
        try {
            final PrintWriter pw1 = new PrintWriter(confFile);
            if (!useDefaultScheme) {
                pw1.println(configFileScheme);
            }
            pw1.close();
            final PrintWriter pwTest = new PrintWriter(testFile);
            pwTest.close();
        } catch (FileNotFoundException e) {
            fail(e.getMessage());
        }
        Configuration conf = GlobalConfiguration.loadConfiguration(tmpDir.getAbsolutePath());
        try {
            FileSystem.setDefaultScheme(conf);
            // remove the scheme.
            String noSchemePath = testFile.toURI().getPath();
            URI uri = new URI(noSchemePath);
            // check if the scheme == null (so that we get the configuration one.
            assertTrue(uri.getScheme() == null);
            // get the filesystem with the default scheme as set in the confFile1
            FileSystem fs = useExplicitScheme ? FileSystem.get(testFile.toURI()) : FileSystem.get(uri);
            assertTrue(fs.exists(new Path(noSchemePath)));
        } catch (IOException e) {
            fail(e.getMessage());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    } finally {
        try {
            // clear the default scheme set in the FileSystem class.
            // we do it through reflection to avoid creating a publicly
            // accessible method, which could also be wrongly used by users.
            Field f = FileSystem.class.getDeclaredField("defaultScheme");
            f.setAccessible(true);
            f.set(null, null);
        } catch (IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
            fail("Cannot reset default scheme: " + e.getMessage());
        }
        confFile.delete();
        testFile.delete();
        tmpDir.delete();
    }
}
Also used : Path(org.apache.flink.core.fs.Path) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Field(java.lang.reflect.Field) FileSystem(org.apache.flink.core.fs.FileSystem) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 58 with URISyntaxException

use of java.net.URISyntaxException in project databus by linkedin.

the class DatabusSubscription method createPrettyNameFromSubscription.

/**
   * Given a subscription, the method below constructs a pretty name
   *
   * The expected input/output is of the following formats:
   * 1. subs = ["com.linkedin.events.db.dbPrefix.tableName"]
   *    prettyName = "dbPrefix_tableName"
   *
   * 2. subs = ["com.linkedin.events.db.dbPrefix1.tableName1","com.linkedin.events.db.dbPrefix2.tableName2"],
   *    prettyName = "dbPrefix1_tableName1_dbPrefix2_tableName2"
   *
   * 3. subs =["espresso:/db/1/tableName1"
   *    prettyName = "db_tableName1_1"
   *
   * 4. subs =["espresso:/db/<wildcard>/tableName1"]. where wildcard=*
   *    prettyName = "db_tableName1"
   *
   * 5. subs =["espresso:/db/1/<wildcard>"]. where wildcard=*
   *    prettyName = "db_1"
   */
public String createPrettyNameFromSubscription() {
    String s = generateSubscriptionString();
    URI u = null;
    try {
        u = new URI(s);
    } catch (URISyntaxException e) {
        throw new DatabusRuntimeException("Unable to decode a URI from the string s = " + s + " subscription = " + toString());
    }
    if (null == u.getScheme()) {
        // TODO: Have V2 style subscriptions have an explicit codec type. Make it return "legacy" codec
        // here. Given a subscription string, we should be able to convert it to DatabusSubscription
        // in an idempotent way. That is, converting back and forth should give the same value
        // Subscription of type com.linkedin.databus.events.dbName.tableName
        String[] parts = s.split("\\.");
        int len = parts.length;
        if (len == 0) {
            // Error case
            String errMsg = "Unexpected format for subscription. sub = " + toString() + " string = " + s;
            throw new DatabusRuntimeException(errMsg);
        } else if (len == 1) {
            // Unit-tests case: logicalSource is specified as "source1"
            return parts[0];
        } else {
            // Expected case. com.linkedin.databus.events.dbName.tableName
            String pn = parts[len - 2] + "_" + parts[len - 1];
            return pn;
        }
    } else if (u.getScheme().equals("espresso")) {
        // Given that this subscription conforms to EspressoSubscriptionUriCodec,
        // logicalSourceName (DBName.TableName) and partitionNumber(1) are guaranteed to be non-null
        String dbName = getPhysicalPartition().getName();
        boolean isWildCardOnTables = getLogicalSource().isAllSourcesWildcard();
        String name = getLogicalPartition().getSource().getName();
        boolean isWildCardOnPartitions = getPhysicalPartition().isAnyPartitionWildcard();
        String pId = getPhysicalPartition().getId().toString();
        StringBuilder sb = new StringBuilder();
        sb.append(dbName);
        if (!isWildCardOnTables) {
            sb.append("_");
            String[] parts = name.split("\\.");
            assert (parts.length == 2);
            sb.append(parts[1]);
        }
        if (!isWildCardOnPartitions) {
            sb.append("_");
            sb.append(pId);
        }
        s = sb.toString();
    } else {
        String errMsg = "The subscription object described as " + toString() + " is not of null or espresso type codec";
        throw new DatabusRuntimeException(errMsg);
    }
    return s;
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException)

Example 59 with URISyntaxException

use of java.net.URISyntaxException in project databus by linkedin.

the class LegacySubscriptionUriCodec method encode.

@Override
public URI encode(DatabusSubscription sub) {
    try {
        String uriSsc = sub.generateSubscriptionString();
        ;
        URI result = uriSsc.indexOf(':') >= 0 ? new URI(getScheme(), uriSsc, null) : new URI(uriSsc);
        return result;
    } catch (URISyntaxException e) {
        throw new RuntimeException("unable to generate legacy subscription URI: " + e.getMessage(), e);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 60 with URISyntaxException

use of java.net.URISyntaxException in project blade by biezhi.

the class ReflectKit method scanPackageClass.

// ------------------------------------------------------
/** 扫描包下面所有的类 */
public static List<String> scanPackageClass(String rootPackageName) {
    List<String> classNames = new ArrayList<String>();
    try {
        ClassLoader loader = ReflectKit.class.getClassLoader();
        URL url = loader.getResource(rootPackageName.replace('.', '/'));
        ExceptionKit.makeRunTimeWhen(url == null, "package[%s] not found!", rootPackageName);
        String protocol = url.getProtocol();
        if ("file".equals(protocol)) {
            LOGGER.debug("Scan in file ...");
            File[] files = new File(url.toURI()).listFiles();
            for (File f : files) {
                scanPackageClassInFile(rootPackageName, f, classNames);
            }
        } else if ("jar".equals(protocol)) {
            LOGGER.debug("Scan in jar ...");
            JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
            scanPackageClassInJar(jar, rootPackageName, classNames);
        }
    } catch (URISyntaxException e) {
    } catch (IOException e) {
    }
    return classNames;
}
Also used : ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

Aggregations

URISyntaxException (java.net.URISyntaxException)1633 URI (java.net.URI)1080 IOException (java.io.IOException)451 URL (java.net.URL)287 File (java.io.File)280 ArrayList (java.util.ArrayList)146 MalformedURLException (java.net.MalformedURLException)102 InputStream (java.io.InputStream)93 HashMap (java.util.HashMap)91 Test (org.testng.annotations.Test)88 Response (javax.ws.rs.core.Response)87 Builder (javax.ws.rs.client.Invocation.Builder)84 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)84 Parameters (org.testng.annotations.Parameters)84 BaseTest (org.xdi.oxauth.BaseTest)84 ResponseType (org.xdi.oxauth.model.common.ResponseType)84 AuthorizationRequest (org.xdi.oxauth.client.AuthorizationRequest)78 Test (org.junit.Test)75 REGISTRATION_CLIENT_URI (org.xdi.oxauth.model.register.RegisterResponseParam.REGISTRATION_CLIENT_URI)72 Intent (android.content.Intent)63