Search in sources :

Example 1 with UnexpectedException

use of org.osgl.exception.UnexpectedException in project actframework by actframework.

the class Act method startup.

public static void startup(AppDescriptor descriptor) {
    processEnvironment(descriptor);
    Banner.print(descriptor);
    loadConfig();
    initMetricPlugin();
    initPluginManager();
    initAppServicePluginManager();
    initDbManager();
    initEnhancerManager();
    initViewManager();
    initAppCodeScannerPluginManager();
    loadPlugins();
    // so it can order the enhancers
    enhancerManager().registered();
    initNetworkLayer();
    initApplicationManager();
    LOGGER.info("loading application(s) ...");
    appManager.loadSingleApp(descriptor);
    startNetworkLayer();
    Thread.currentThread().setContextClassLoader(Act.class.getClassLoader());
    App app = app();
    if (null == app) {
        shutdownNetworkLayer();
        throw new UnexpectedException("App not found. Please make sure your app start directory is correct");
    }
    emit(SysEventId.ACT_START);
    writePidFile();
}
Also used : RunApp(act.boot.app.RunApp) UnexpectedException(org.osgl.exception.UnexpectedException)

Example 2 with UnexpectedException

use of org.osgl.exception.UnexpectedException in project actframework by actframework.

the class JsonDTOClassManager method extractBeanSpec.

private void extractBeanSpec(List<BeanSpec> beanSpecs, Method method, Class host) {
    Type[] paramTypes = method.getGenericParameterTypes();
    int sz = paramTypes.length;
    if (0 == sz) {
        return;
    }
    Annotation[][] annotations = method.getParameterAnnotations();
    for (int i = 0; i < sz; ++i) {
        Type type = paramTypes[i];
        if (type instanceof TypeVariable && !Modifier.isStatic(method.getModifiers())) {
            // explore type variable impl
            TypeVariable typeVar = $.cast(type);
            String typeVarName = typeVar.getName();
            // find all generic types on host
            Map<String, Class> typeVarLookup = Generics.buildTypeParamImplLookup(host);
            type = typeVarLookup.get(typeVarName);
            if (null == type) {
                throw new UnexpectedException("Cannot determine concrete type of method parameter %s", typeVarName);
            }
        }
        Annotation[] anno = annotations[i];
        BeanSpec spec = BeanSpec.of(type, anno, injector);
        if (ParamValueLoaderService.providedButNotDbBind(spec, injector)) {
            continue;
        }
        String dbBindName = dbBindName(spec);
        if (null != dbBindName) {
            beanSpecs.add(BeanSpec.of(String.class, new Annotation[0], dbBindName, injector));
        } else {
            beanSpecs.add(spec);
        }
    }
}
Also used : UnexpectedException(org.osgl.exception.UnexpectedException) BeanSpec(org.osgl.inject.BeanSpec) Annotation(java.lang.annotation.Annotation)

Example 3 with UnexpectedException

use of org.osgl.exception.UnexpectedException in project actframework by actframework.

the class ResourceLoader method _load.

protected static Object _load(String resourcePath, BeanSpec spec, boolean ignoreResourceNotFound) {
    URL url = loadResource(resourcePath);
    if (null == url) {
        if (!ignoreResourceNotFound) {
            LOGGER.warn("resource not found: " + resourcePath);
        }
        return null;
    }
    boolean isJson = resourcePath.endsWith(".json");
    Class<?> rawType = spec.rawType();
    if (URL.class == rawType) {
        return url;
    } else if (String.class == rawType) {
        return IO.readContentAsString(url);
    } else if (byte[].class == rawType) {
        return readContent(url);
    } else if (List.class.equals(rawType)) {
        List<Type> typeParams = spec.typeParams();
        if (!typeParams.isEmpty()) {
            if (String.class == typeParams.get(0)) {
                return IO.readLines(url);
            } else if (isJson) {
                return JSON.parseObject(IO.readContentAsString(url), spec.type());
            }
        }
    } else if (Collection.class.isAssignableFrom(rawType)) {
        List<Type> typeParams = spec.typeParams();
        if (!typeParams.isEmpty()) {
            Collection col = (Collection) Act.getInstance(rawType);
            if (String.class == typeParams.get(0)) {
                col.addAll(IO.readLines(url));
                return col;
            } else {
                StringValueResolverManager resolverManager = Act.app().resolverManager();
                try {
                    Class componentType = spec.componentSpec().rawType();
                    List<String> stringList = IO.readLines(url);
                    for (String line : stringList) {
                        col.add(resolverManager.resolve(line, componentType));
                    }
                } catch (RuntimeException e) {
                    throw new UnexpectedException("return type not supported: " + spec);
                }
            }
        }
    } else if (ByteBuffer.class == rawType) {
        byte[] ba = readContent(url);
        ByteBuffer buffer = ByteBuffer.allocateDirect(ba.length);
        buffer.put(ba);
        buffer.flip();
        return buffer;
    } else if (Path.class.isAssignableFrom(rawType)) {
        try {
            return Paths.get(url.toURI());
        } catch (URISyntaxException exception) {
            throw E.unexpected(exception);
        }
    } else if (File.class.isAssignableFrom(rawType)) {
        return new File(url.getFile());
    } else if (ISObject.class.isAssignableFrom(rawType)) {
        return SObject.of(readContent(url));
    } else if (InputStream.class == rawType) {
        return IO.is(url);
    } else if (Reader.class == rawType) {
        return new InputStreamReader(IO.is(url));
    }
    if (isJson) {
        return JSON.parseObject(IO.readContentAsString(url), spec.type());
    }
    try {
        return Act.app().resolverManager().resolve(IO.readContentAsString(url), rawType);
    } catch (RuntimeException e) {
        throw new UnexpectedException("return type not supported: " + spec);
    }
}
Also used : UnexpectedException(org.osgl.exception.UnexpectedException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL) Type(java.lang.reflect.Type) StringValueResolverManager(act.app.data.StringValueResolverManager) Collection(java.util.Collection) List(java.util.List) File(java.io.File)

Example 4 with UnexpectedException

use of org.osgl.exception.UnexpectedException in project actframework by actframework.

the class ApacheMultipartParser method parse.

@Override
public Map<String, String[]> parse(ActionContext context) {
    H.Request request = context.req();
    InputStream body = request.inputStream();
    Map<String, String[]> result = new HashMap<>();
    try {
        FileItemIteratorImpl iter = new FileItemIteratorImpl(body, request.header("content-type"), request.characterEncoding());
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            ISObject sobj = UploadFileStorageService.store(item, context.app());
            if (sobj.getLength() == 0L) {
                continue;
            }
            String fieldName = item.getFieldName();
            if (item.isFormField()) {
                // must resolve encoding
                // this is our default
                String _encoding = request.characterEncoding();
                String _contentType = item.getContentType();
                if (_contentType != null) {
                    ContentTypeWithEncoding contentTypeEncoding = ContentTypeWithEncoding.parse(_contentType);
                    if (contentTypeEncoding.encoding != null) {
                        _encoding = contentTypeEncoding.encoding;
                    }
                }
                mergeValueInMap(result, fieldName, sobj.asString(Charset.forName(_encoding)));
            } else {
                context.addUpload(item.getFieldName(), sobj);
                mergeValueInMap(result, fieldName, fieldName);
            }
        }
    } catch (FileUploadIOException e) {
        throw E.ioException("Error when handling upload", e);
    } catch (IOException e) {
        throw E.ioException("Error when handling upload", e);
    } catch (FileUploadException e) {
        throw E.ioException("Error when handling upload", e);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new UnexpectedException(e);
    }
    return result;
}
Also used : UnexpectedException(org.osgl.exception.UnexpectedException) HashMap(java.util.HashMap) LimitedInputStream(org.apache.commons.fileupload.util.LimitedInputStream) InputStream(java.io.InputStream) H(org.osgl.http.H) ISObject(org.osgl.storage.ISObject) IOException(java.io.IOException) IOException(java.io.IOException) UnexpectedException(org.osgl.exception.UnexpectedException) NoSuchElementException(java.util.NoSuchElementException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with UnexpectedException

use of org.osgl.exception.UnexpectedException in project actframework by actframework.

the class TextParser method parse.

@Override
public Map<String, String[]> parse(ActionContext context) {
    H.Request req = context.req();
    InputStream is = req.inputStream();
    try {
        Map<String, String[]> params = new HashMap<String, String[]>();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int b;
        while ((b = is.read()) != -1) {
            os.write(b);
        }
        byte[] data = os.toByteArray();
        params.put(ActionContext.REQ_BODY, data.length == 0 ? null : new String[] { new String(data, req.characterEncoding()) });
        return params;
    } catch (Exception e) {
        throw new UnexpectedException(e);
    }
}
Also used : UnexpectedException(org.osgl.exception.UnexpectedException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) H(org.osgl.http.H) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnexpectedException(org.osgl.exception.UnexpectedException)

Aggregations

UnexpectedException (org.osgl.exception.UnexpectedException)7 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)3 H (org.osgl.http.H)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BeanSpec (org.osgl.inject.BeanSpec)2 StringValueResolverManager (act.app.data.StringValueResolverManager)1 RunApp (act.boot.app.RunApp)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1 Charset (java.nio.charset.Charset)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1