com.intellij.util.net.NetUtils Java Examples

The following examples show how to use com.intellij.util.net.NetUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExternalSystemRunConfiguration.java    From consulo with Apache License 2.0 6 votes vote down vote up
public MyRunnableState(@Nonnull ExternalSystemTaskExecutionSettings settings, @Nonnull Project project, boolean debug) {
  mySettings = settings;
  myProject = project;
  int port;
  if (debug) {
    try {
      port = NetUtils.findAvailableSocketPort();
    }
    catch (IOException e) {
      LOG.warn("Unexpected I/O exception occurred on attempt to find a free port to use for external system task debugging", e);
      port = 0;
    }
  }
  else {
    port = 0;
  }
  myDebugPort = port;
}
 
Example #2
Source File: CommonProxy.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public List<Proxy> select(@Nullable URI uri) {
  isInstalledAssertion();
  if (uri == null) {
    return NO_PROXY_LIST;
  }
  LOG.debug("CommonProxy.select called for " + uri.toString());

  if (Boolean.TRUE.equals(ourReenterDefence.get())) {
    return NO_PROXY_LIST;
  }
  try {
    ourReenterDefence.set(Boolean.TRUE);
    String host = StringUtil.notNullize(uri.getHost());
    if (NetUtils.isLocalhost(host)) {
      return NO_PROXY_LIST;
    }

    final HostInfo info = new HostInfo(uri.getScheme(), host, correctPortByProtocol(uri));
    final Map<String, ProxySelector> copy;
    synchronized (myLock) {
      if (myNoProxy.contains(Pair.create(info, Thread.currentThread()))) {
        LOG.debug("CommonProxy.select returns no proxy (in no proxy list) for " + uri.toString());
        return NO_PROXY_LIST;
      }
      copy = new THashMap<>(myCustom);
    }
    for (Map.Entry<String, ProxySelector> entry : copy.entrySet()) {
      final List<Proxy> proxies = entry.getValue().select(uri);
      if (!ContainerUtil.isEmpty(proxies)) {
        LOG.debug("CommonProxy.select returns custom proxy for " + uri.toString() + ", " + proxies.toString());
        return proxies;
      }
    }
    return NO_PROXY_LIST;
  }
  finally {
    ourReenterDefence.remove();
  }
}
 
Example #3
Source File: HttpRequests.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public byte[] readBytes(@Nullable ProgressIndicator indicator) throws IOException {
  int contentLength = getConnection().getContentLength();
  BufferExposingByteArrayOutputStream out = new BufferExposingByteArrayOutputStream(contentLength > 0 ? contentLength : BLOCK_SIZE);
  NetUtils.copyStreamContent(indicator, getInputStream(), out, contentLength);
  return ArrayUtil.realloc(out.getInternalBuffer(), out.size());
}
 
Example #4
Source File: HttpRequests.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public File saveToFile(@Nonnull File file, @Nullable ProgressIndicator indicator) throws IOException {
  FileUtilRt.createParentDirs(file);

  boolean deleteFile = true;
  try {
    OutputStream out = new FileOutputStream(file);
    try {
      NetUtils.copyStreamContent(indicator, getInputStream(), out, getConnection().getContentLength());
      deleteFile = false;
    }
    catch (IOException e) {
      throw new IOException(createErrorMessage(e, this, false), e);
    }
    finally {
      out.close();
    }
  }
  finally {
    if (deleteFile) {
      FileUtilRt.delete(file);
    }
  }

  return file;
}
 
Example #5
Source File: BuiltInServerManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isOnBuiltInWebServerByAuthority(@Nonnull String authority) {
  int portIndex = authority.indexOf(':');
  if (portIndex < 0 || portIndex == authority.length() - 1) {
    return false;
  }

  int port = StringUtil.parseInt(authority.substring(portIndex + 1), -1);
  if (port == -1) {
    return false;
  }

  BuiltInServerOptions options = BuiltInServerOptions.getInstance();
  int idePort = BuiltInServerManager.getInstance().getPort();
  if (options.builtInServerPort != port && idePort != port) {
    return false;
  }

  String host = authority.substring(0, portIndex);
  if (NetUtils.isLocalhost(host)) {
    return true;
  }

  try {
    InetAddress inetAddress = InetAddress.getByName(host);
    return inetAddress.isLoopbackAddress() ||
           inetAddress.isAnyLocalAddress() ||
           (options.builtInServerAvailableExternally && idePort != port && NetworkInterface.getByInetAddress(inetAddress) != null);
  }
  catch (IOException e) {
    return false;
  }
}
 
Example #6
Source File: HttpUrlConnectionUtil.java    From leetcode-editor with Apache License 2.0 4 votes vote down vote up
public static  BufferExposingByteArrayOutputStream readBytes( InputStream inputStream, @NotNull URLConnection connection, @Nullable ProgressIndicator progressIndicator) throws IOException, ProcessCanceledException {
    int contentLength = connection.getContentLength();
    BufferExposingByteArrayOutputStream out = new BufferExposingByteArrayOutputStream(contentLength > 0 ? contentLength : BLOCK_SIZE);
    NetUtils.copyStreamContent(progressIndicator, inputStream, (OutputStream)out, contentLength);
    return out;
}
 
Example #7
Source File: XQueryDebuggerRunner.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
private int getAvailablePort() {
    return NetUtils.tryToFindAvailableSocketPort(9000);
}
 
Example #8
Source File: SubServer.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean bind(int port) {
  if (port == server.getPort() || port == -1) {
    return true;
  }

  if (channelRegistrar == null) {
    Disposer.register(server, this);
    channelRegistrar = new ChannelRegistrar();
  }

  ServerBootstrap bootstrap = serverBootstrap(server.getEventLoopGroup());
  Map<String, Object> xmlRpcHandlers = user.createXmlRpcHandlers();
  if (xmlRpcHandlers == null) {
    BuiltInServer.configureChildHandler(bootstrap, channelRegistrar, null);
  }
  else {
    final XmlRpcDelegatingHttpRequestHandler handler = new XmlRpcDelegatingHttpRequestHandler(xmlRpcHandlers);
    bootstrap.childHandler(new ChannelInitializer() {
      @Override
      protected void initChannel(Channel channel) throws Exception {
        channel.pipeline().addLast(channelRegistrar);
        NettyUtil.addHttpServerCodec(channel.pipeline());
        channel.pipeline().addLast(handler);
      }
    });
  }

  try {
    bootstrap.localAddress(user.isAvailableExternally() ? new InetSocketAddress(port) : NetUtils.loopbackSocketAddress(port));
    channelRegistrar.setServerChannel(bootstrap.bind().syncUninterruptibly().channel(), false);
    return true;
  }
  catch (Exception e) {
    try {
      NettyUtil.log(e, Logger.getInstance(BuiltInServer.class));
    }
    finally {
      user.cannotBind(e, port);
    }
    return false;
  }
}