PRAGUE — For more than 1,000 years, the sprawling castle complex perched high above Prague has been the seat of power for Holy Roman emperors, the kings of Bohemia and, now, the Czech president, Milos Zeman. And for the last four years, the Chinese technology giant Huawei has had a contract to fulfill the communication needs of the president and his staff.

The presidential contract is the most visible symbol of how deeply Huawei has established itself in the Czech Republic, long viewed by China as a springboard country for its interests across the European Union.

So when the Czech government’s cybersecurity agency issued a directive in December warning that Huawei represented a potential national security threat, company officials were shocked — as was Mr. Zeman, known for his closeness to China. Huawei has threatened legal and financial retaliation. Mr. Zeman has accused his own intelligence services, including the cybersecurity agency, known as Nukib, of “dirty tricks.”

The unexpected confrontation in the Czech Republic comes as Huawei, already entangled in the trade war between China and the United States, is running into deepening problems in European Union countries, where it has worked for years to build inroads. Only weeks after Nukib issued its directive against Huawei, Polish authorities in January arrested a Huawei employee in Warsaw on charges of spying.